PHP - Better Solution To Group An Array By An Specific Key
Hi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays.
I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) );
and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 )
Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; }
The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>';
Thank you very much for your ideas! Mapg Similar TutorialsThis topic has been moved to Microsoft SQL - MSSQL. http://www.phpfreaks.com/forums/index.php?topic=347695.0 Hi All, I hope i'm posting in the right place but also hope someone can help! I've got the following code: <?php $variations = $product->get_available_variations(); if ($variations): ?> <div class="table-responsive"> <table class="table table-striped"> <tbody> <?php foreach ($variations as $key => $value) : ?> <tr> <td><?php echo $value['sku']; ?> <?php echo $value['variation_id']; ?></td> <?php foreach ($value['attributes'] as $attrKey => $attr) : $tax = str_replace('attribute_', '', $attrKey); $term_obj = get_term_by('slug', $attr, $tax); ?> <td><?php echo $term_obj->name; ?></td> <?php endforeach; ?> </tr> <?php endforeach; #$variations?> </tbody> </table> </div><!-- /table-responsive --> <?php endif; #$variations ?> This produces the following table: Product code System Pack Quantity Variation XT1ECWH 234 System 1 2 N/A XT2ECLWH 236 System 2 2 Left XT2ECRWH 237 System 2 2 Right XT3ECWH 238 System 3 2 N/AHowever, is it possible to alter the code to group the information? So it looks like the below? System 1 System 2 System 3 System 1XT1ECWH 234 Pack Quantity: 2 Variation: N/A System 2 XT2ECLWH 236 Pack Quantity: 2 Variation: Left XT2ECRWH 237 Pack Quantity: 2 Variation: Right System 3 Sorry, not sure why it's not formatted correctly but hopefully you can see what I'm trying to achieve. The array for $variations is as follows: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [sku] => XT1ECWH [variation_description] => [variation_id] => 234 ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => left ) [sku] => XT2ECLWH [variation_description] => Left [variation_id] => 236 ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => right ) [sku] => XT2ECRWH [variation_description] => Right [variation_id] => 237 ) [3] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [price_html] => [sku] => XT3ECWH [variation_description] => ) ) If it helps, I need to always group by the first attribute, in this case : attribute_pa_system Any help on this would be very much appreciated! Thank you in advanced. hi, i'm trying to sort an array of associative arrays, based on one of the keys. if the value of the key is a primitive, normal sorting occurs - this works fine. if the value of the key is an array, i'd like to have it sorted by "groups" here's an example: Code: [Select] <?php $data[] = array('name' => 'h', 'list' => array(1,2)); $data[] = array('name' => 'g', 'list' => array(1)); $data[] = array('name' => 'a', 'list' => array(1,3)); $data[] = array('name' => 'f', 'list' => array(2)); $data[] = array('name' => 'e', 'list' => array(2,3)); $data[] = array('name' => 'b', 'list' => array(3)); $data[] = array('name' => 'c', 'list' => array(1,2,3,4)); $data[] = array('name' => 'd', 'list' => array(3,4)); function deep_sort($array, $sorton){ usort($array, function($a, $b) use($sorton) { $a = $a[$sorton]; $b = $b[$sorton]; if(is_array($a) && is_array($b)){ // this bit is obviously flawed - only included for illustrative purposes $a = implode('', $a); $b = implode('', $b); } return ($a == $b) ? 0 : ($a > $b) ? 1 : -1; }); return $array; } $sorted_by_name = deep_sort($data, 'name'); print '<pre>'; print_r($sorted_by_name); print '</pre>'; $sorted_by_list = deep_sort($data, 'list'); print '<pre>'; print_r($sorted_by_list); print '</pre>'; ?> the sorted_by_name bit is fine - but for sorted_by_list, what i want returned is all the array ordered as follows: all those with "1" (or the lowest if none have "1") in it's list value first, then all those remaining that have the next highest ("2"), etc. so right now, the returns for sort_by_list look like this: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [2] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [3] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [4] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) [7] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) )whereas i'd like to get back: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [2] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) [3] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [4] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [7] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) ) TYIA for any suggestions I have been struggling with this for awhile now, and I think it isn't as hard as I making it. Basically I want to "group" my array by month making a multidimensional array by months. Basically right now my print_r($my_array) outputs: Code: [Select] Array ( [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [5] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) I need to take that and make it look like this, grouping by the month of the 'dates' value: Code: [Select] Array ( [0] => Array ( //June [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) [1] => Array ( //July [0] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) ) Hi all. I am currently trying to build a site that will allow users to manage a self build house project. This is my first php MySQL project but I am getting to grips with it ok. I am currently building this for my own benefit at the moment as I am about to undertake a self build. I have a few questions to put to you guys. This is where I am at the moment I have a database table named materials which contains a list of materials , quantities, unit of measurement, id. My site has a user control panel that has separate forms for each stage of the build where the user enters the quantities of the materials, and the data is displayed as an array for each stage. So, heres question 1. I want to take the data and use it to generate a form which will be completed by several merchants.... so it will display similar to the array but will be a form with a text box for price where the merchant will insert a price. i.e <?php mysql_connect ("localhost", "test", "test") or die ('I cannot connect to the database becuase: ' . mysql_error()); mysql_select_db ("matquant"); // query $query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); // results while ($row = mysql_fetch_array($query)) { echo "<br />" .$row ['id']. " " .$row ['material']. " " .$row['quantity']. " " .$row['unit']. "<br />";} echo mysql_error(); ?> I want to add a text input box form which will allow the merchant to add a price to each item that is added to the array. so there the merchant will be able to give me a price for all the items in my list. Heres where I get stuck.... as I am constantly adding to the list I have no fixed amount of items so the form for the merchants needs to be built dynamically, ie each time I add a new material, I want the merchants form to get a new input. I am not very good at describing my problems but I am hoping that someone can help me out from the information given. Thats the first question. i like to get issues sorted in small bites. the second is the next big bite, I want to send the form above to several merchants, so I intend to have a user database with a user for each merchant, and link the tables so that each merchant can provide a price for each material, ideally the form would highlight the materials that have not had a price yet. I'm looking for advice on the best method of doing this basically but if I can get the first question sorted that would be great.. I have the test site local but can upload it it to the server if it will help you see what im trying to do. Any help that anyone can give will be greatly appreciated. Hi All, I've a loop which creates an array as follows: $product_table[] = ['SKU' => $sku, 'Label' => $attribute_name, 'Value' => $term_obj->name ]; I'm then grouping the data by SKU code: #group the products by SKU $group_products = array(); foreach ($product_table as $element) : $group_products[$element['SKU']][] = $element; endforeach; Finally, I output the data: #output the data foreach ($group_products as $itemName => $rows) : echo '<tr>'; #echo '<td>', $element['SKU'], '</td>'; $i=0; foreach ($rows as $row) : $i++; #echo '<td>'. $row["SKU"]. '</td><td>'. $row["Label"]. '</td><td>'. $row["Value"]. '</td>'; if ($i == 1): echo '<td>'. $row["SKU"]. '</td><td>'. $row["Value"]. '</td>'; else: echo '<td>'. $row["Value"]. '</td>'; endif; #echo '<td>'. $row["Value"]. '</td>'; endforeach; echo '</tr>'; endforeach; ?> And looks like: Product code System Pack Quantity XT1CWH System 1 1 x 3m XT2CWH System 2 1 x 3m XT3CWH System 3 1 x 3m
This works perfectly fine. However, some products share the same SKU and therefore it causes an issue, like the below: Product code System Pack Quantity XT1CLWH System 1 8 x 3m System 2 8 x 3m System 3 8 x 3m Is there a way I can avoid this, so if perhaps creates the new row but shows the same SKU code? Many thanks I have it to where it groups two parts of the Array randomly but I need to make it where it won't pair the same together, or if it has already been paired with a team then use another team. Each value of the array can only be used once. Here is my code shuffle($teamname); $loopCount = $games; for ($i = 0; $i < $loopCount; $i++) { $player1 = array_pop($teamname); $player2 = array_pop($teamname); if(is_null($player1)) { echo 'Not enough different teams to create '.$games.' games'; break; } // output to screen echo "Team: " . $player1 . " Vs Team: " . $player2 . "<br />"; } } Hi guys, I'm using the API for Remember the Milk through a PHP class that I found online called PHP-RTM. Anyway after so much headaches (the authentication for the Remember the Milk API is one of the hardest I've ever come across) I've finally connected everything and gotten to the stage that I have some raw data inside an array. The next step is to work with that data. Basically I want to access the names of the task in a complex multi demensional array and I have no idea how to do this. I have done a var_dump of the array I am working with to show you the complexity. Just remember that in the complex var_dump below there are only 4 tasks: array(2) { ["stat"]=> string(2) "ok" ["tasks"]=> array(2) { ["rev"]=> string(31) "nxeir6vy9pcgcggoss4wwwooc8cwo8w" ["list"]=> array(3) { [/tt][list][li][tt]=> array(2) { ["id"]=> string( 8) "13927305" ["taskseries"]=> array(2) { [/tt][/li][li][tt]=> array(11) { ["id"]=> string(9) "104056511" ["created"]=> string(20) "2011-02-02T12:48:48Z" ["modified"]=> string(20) "2011-02-02T12:48:48Z" ["name"]=> string(42) "Send confirmation email to luxury car hire" ["source"]=> string(13) "iphone-native" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["tags"]=> array(0) { } ["participants"]=> array(0) { } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156815180" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-02T12:48:48Z" ["completed"]=> string(0) "" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } [1]=> array(11) { ["id"]=> string(9) "104148150" ["created"]=> string(20) "2011-02-03T01:25:57Z" ["modified"]=> string(20) "2011-02-03T01:26:17Z" ["name"]=> string(40) "Confirm that post has been sent with DVD" ["source"]=> string(2) "js" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["tags"]=> array(0) { } ["participants"]=> array(0) { } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156951316" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-03T01:25:57Z" ["completed"]=> string(0) "" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } } } [1]=> array(2) { ["id"]=> string( 8) "13925842" ["taskseries"]=> array(12) { ["id"]=> string( 8) "98105089" ["created"]=> string(20) "2010-12-19T11:25:25Z" ["modified"]=> string(20) "2011-02-02T13:01:42Z" ["name"]=> string(25) "Read for one hour per day" ["source"]=> string(13) "iphone-native" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["rrule"]=> array(2) { ["every"]=> string(1) "1" ["$t"]=> string(21) "INTERVAL=1;FREQ=DAILY" } ["tags"]=> array(0) { } ["participants"]=> array(0) { } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156576423" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-01T13:01:45Z" ["completed"]=> string(0) "" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } } [2]=> array(2) { ["id"]=> string( 8) "17007566" ["taskseries"]=> array(11) { ["id"]=> string(9) "104017503" ["created"]=> string(20) "2011-02-02T05:45:04Z" ["modified"]=> string(20) "2011-02-03T02:29:55Z" ["name"]=> string(96) "Hook up watermark for both videos (check email) and send to Rachelle and Steve at compare quotes" ["source"]=> string(2) "js" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["tags"]=> array(0) { } ["participants"]=> array(1) { ["contact"]=> array(3) { ["id"]=> string(7) "2150286" ["fullname"]=> string(10) "Samuel Nam" ["username"]=> string(10) "samuel.nam" } } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156754354" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-02T05:45:04Z" ["completed"]=> string(20) "2011-02-03T02:32:00Z" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } } } } } Anyone know the code i can use to simply get the "name" keys from the complex array below. I've created a movie to show what I'm trying to achieve, please click below to view: http://screenr.com/XIY MOD EDIT: formatted text to get rid of errant smilies . . . [/list] okay I am using a jquery plugin to re-arrange images which are in mysql and they have their own "order_number" ... when i re-arrange stuff the new order is put into a hidden field in a form and i have it along with another hidden field with their current order... when submitted I combine these two fields in an array so the key is the original order and the value is the new order ....i have 12 images... Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 ) this is what is what the array looks like... i have a foreach loop with a mysql query but things get kind of weird when you try to do it this way $num = $_POST['old_num']; $num = unserialize($num); $sort = $_POST['sortOrder']; $sort = explode(',', $sort); if ($_POST){ $combine = array_combine($num, $sort); foreach($combine as $key => $value){ mysql_query("UPDATE shirts SET order_number='$value' WHERE order_number='$key' ") or die(mysql_error()); } when you do this lets say for example we have the image with order number "1" get switched with the image order number "2"..... the database first updates with number 1 which becomes 2... then when it goes to number "2"... at this point... the first image gets switched back to number 1.... so 1 becomes 2 then the new 2 becomes 1 again and the other 2 becomes 1 also. I can not think of another way I could update mysql with the correct order numbers... If anyone has any ideas or other solutions I am open to all suggestions... thank you! I have an array that looks like this:
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "2021-02-10 09:06:18" [7]=> string(19) "2021-02-10 09:06:24" }
I need to group the instances any time there is more than a 6 second gap between elements. So 0 =>5 would be one array and 6 and 7 would be a new array.
Ive been able to produce the following but its not ideal
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "end" [7]=> string(19) "2021-02-10 09:06:18" [8]=> string(19) "2021-02-10 09:06:24" }
$burner_time = array(); foreach($Burner_Control_Alm as $new_burner){ $burner_time[] = strtotime($new_burner); } $repl = 'end'; for ($i=1; $i<count($burner_time); $i++) { $value_second = $burner_time[$i]; $value_first = $burner_time[$i-1] === $repl ? $burner_time[$i-2] : $burner_time[$i-1]; if ($value_second > $value_first + 6) { array_splice($burner_time, $i++, 0, $repl); } print_r($burner_time); } I have users in my database with many stored points ie age, gender, interests etc. about 20 points in all.
I have a table of products that I want to recommend only applicable products. They each have saved like minage, maxage, gender, interest etc for the ideal consumer. ie dress gender=f
This is the bare bones of what I have so far:
$result = mysql_query("SELECT * FROM user_table WHERE hash='$session_id'"); $rowuser = mysql_fetch_array($result); $result = mysql_query("SELECT * FROM products"); $rowproducts = mysql_fetch_array($result);So now I have $rowuser['gender'] = m , how do I remove all from $rowproducts where gender = f? Is there a best way to do this knowing I have about 20 points to go through before I am left with an array with just the best selection of products for this user in it? I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); Hey guys. How do you remove a specific element from an array? Lets say I have a string like this: $list = "guy,girl,boy,man,woman,lady"; Then I use explode to break it up into an array by commas: $listArray = explode(",",$list); Now $listArray contains everything. Now if I wanted to delete something specific from that array, how would I do that? So for example, I want to delete "man" out of the array. How do I do that without know its key? Hello all, I have a rather large array and I need to list it's contents in an alphabetical list. The array is built like this: $array['query']['row']['0']['title'] = 'Bob'; $array['query']['row']['1']['title'] = 'Andy'; $array['query']['row']['2']['title'] = 'Tom'; I need to sort it so it is in alphabetical order based on the title. Is there an easy way to do this? <?php class CsvToArray { /* global $file; $file = '../Datafeeds/baseball_all.csv'; */ public function data() { $file = '../Datafeeds/baseball_all.csv'; $fileName = fopen($file,'r+'); $all_rows = array(); $header = fgetcsv($fileName); while ($row = fgetcsv($fileName)) { $games[] = array_combine($header, $row); } //print_r($games); print_r($games[64]["Home Team"]); } } $game = new CsvToArray(); $game->data(); ?> csv: https://anonymousfiles.io/vSubWgPY/ How can I get specific array values outside a class or function? About to try "File put contents" I think. That should work right? So I thought the last couple times. Or what would you suggest?. Overwritten previous attempts to share =[
Next up is seeing if I can change $file to $var to change it from other locations. Should be easy I reckon. Edited April 19 by MinzerHello, i retrieve an array from the database that looks something like this: $var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}}; I need to retrieve the number corresponding to the character i: and print it(11 in the first case). For example , If, $var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}}; I would need to print 15. $var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}}; I would need to print 3. Thanks in advance. Hi guys, I am getting data from a json source: ``` $jsondata = file_get_contents($url);$response = json_decode($jsondata, true) echo "<pre>"; print_r(array_keys($response));; echo "</pre>"; ``` and this returns: ``` Array ( [0] => jsonapi [1] => data [2] => links ) ``` I want to extract data from specific key in the data array (eg 'title'): ``` [data] => Array ( [0] => Array ( [type] => recipes [id] => d6cd7643-6e44-4d26-9ed9-9da7196cef12 [attributes] => Array ( [internalId] => 7 [isPublished] => 1 [title] => Deep mediterranean quiche [createdAt] => 2019-05-07T13:34:27+0200 [updatedAt] => 2019-05-07T13:34:27+0200 [isPromoted] => 1 [path] => /recipes/deep-mediterranean-quiche [cookingTime] => 30 [difficulty] => medium [ingredients] => Array ( [0] => For the pastry: [1] => 280g plain flour [2] => 140g butter [3] => Cold water [4] => For the filling: [5] => 1 onion [6] => 2 garlic cloves [7] => Half a courgette [8] => 450ml soya milk [9] => 500g grated parmesan [10] => 2 eggs [11] => 200g sun dried tomatoes [12] => 100g feta ) [numberOfservings] => 8 [preparationTime] => 40 [instructions] => ``` How do I loop through the data array and select just the 'title' and 'difficulty' for example? Edited May 12, 2019 by ludo1960I am using session variables to keep track of what is in a shopping cart. I can add the items to the cart quite easily enough. But my problem is that I can't delete a specific item in the cart. Below is a summary of what the array looks like $Item1=array("ProdID"=>"Hat","Color"=>"Not Needed","Size"=>5); $Item4=array("ProdID"=>"Shirt","Color"=>"Green","Size"=>"L"); $Item2=array("ProdID"=>"Pants","Color"=>"Not Needed","Size"=>"32x32"); $Item3=array("ProdID"=>"Socks","Color"=>"Not Needed","Size"=>"Not Needed"); $NumItems=4; $TestArray=array("NumItems"=>$NumItems,1=>$Item1,2=>$Item2,3=>$Item3,4=>$Item4); However when I attempt to delete any of the top level elements (1,2,3,4,NumItems), I either end up deleting only the last element (4) or somehow taking out 2 of them. I know that this must be an easy fix but I've looked at it for so long that I can't seem to figure out what the problem is. below is the code that I use to delete the Item from the array Code: [Select] $ItemNum=$_GET['item']; if ($ItemNum=="All"){ $ItemNum=$_GET['item']; if ($ItemNum=="All"){ unset($TestArray); unset($TestArray['NumItems']); unset($_SESSION['OrderTotal']); //header("Location:Cart.php?act=view"); } else{ $TestArray['NumItems']=$TestArray['NumItems']-1; if($TestArray['NumItems']<1) { unset($_SESSION['OrderTotal']); unset($TestArray); unset($TestArray['NumItems']); } else { unset($TestArray[$ItemNum]); } Can anyone take a look at this and point me in the correct direction? What I am after is to delete any element of the top array and keep the rest. Hi all, I have an array of data. I am looping through this data to generate a CSV file. However, if there are commas in any of the elements then this will throw out the structure of the CSV file. My question is, how do I loop through the array before I use it to strip out any commas and replace with a space? Thanks for your help. I need to find out how many times the "mac" key show is in an array. Code: [Select] Array ( [serial] => Robert [added] => Peter [mac] => 1232091823 [mac] => 7538572375 [mac] =>943820348239804 [place] =>home ) |