fezzik Posted October 23, 2006 Share Posted October 23, 2006 I need help with how to accomplish the following...I have a multidimesional array with an unlimited number of rows and 5 columns called $myArray.The array is created from a database query. I would like to create 2 different displays from this array that are printed to a page. The first display would be grouping all rows which share the same value for a specified column (i.e. $myArray[$row][0]) and then printing them. Similar to:// array data$myArray[1][0] = "sifl";$myArray[1][1] = "apple";$myArray[2][0] = "olly";$myArray[2][1] = "orange";$myArray[3][0] = "sifl";$myArray[3][1] = "pear";$myArray[4][0] = "sifl";$myArray[4][1] = "banana";// printed output would look likesiflapplepearbananaollyorangeThe second display would be similar but it would group rows which shared the same values for specified 2 columns. If anyone has the time to help me possibly create 2 functions to accomplish this task I would appreciate it. Any information is appreciated.Best,Fezzik Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/ Share on other sites More sharing options...
doni49 Posted October 23, 2006 Share Posted October 23, 2006 Can you explain what you're looking for a little more clearly? And show any code you've tried--maybe that'll help me understand what you're after. Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113422 Share on other sites More sharing options...
fezzik Posted October 24, 2006 Author Share Posted October 24, 2006 Sure!So I have a multidimensional array of many rows and 5 columns per row. The data is similar to:$myArray[0][0] = "Autobots";$myArray[0][1] = "Bumblebee";$myArray[0][2] = "VW Bug";$myArray[0][3] = "Yellow";$myArray[0][4] = "Smog Check";$myArray[0][0] = "Autobots";$myArray[0][1] = "Jazz";$myArray[0][2] = "Porsche";$myArray[0][3] = "White";$myArray[0][4] = "Steering Realignment";$myArray[0][0] = "Autobots";$myArray[0][1] = "Optimus Prime";$myArray[0][2] = "Diesel";$myArray[0][3] = "Red and Blue";$myArray[0][4] = "Catalytic Converter";$myArray[0][0] = "Decepticons";$myArray[0][1] = "Starscream";$myArray[0][2] = "Flying Machine";$myArray[0][3] = "Red";$myArray[0][4] = "Girly Voice";$myArray[0][0] = "Decepticons";$myArray[0][1] = "Megatron";$myArray[0][2] = "Pea Shooter";$myArray[0][3] = "Purple and Black";$myArray[0][4] = "Everything";I would like to create a function that will cycle through the array and print something similar to (grouping every row by the value of the 1st column):Autobots-----------------BumblebeeJazzOptimus PrimeDecepticons-----------------MegatronStarscreamThus grouping and ordering the information that is printed by the value of the first column of the row. Here is my coding attempt:[code]// region$previousRegion = "";$currentRegion = "";// company$currentCompany = "";$previousCompany = "";// counter$tradeCounter = 0;foreach ($tradeInfo as $book) {foreach ($book as $key => $value){// regionif ( $key == 0 ) { $currentRegion = $value; if ( $currentRegion != $previousRegion ) { if ( $tradeCounter != 0 ) { echo "<br />"; }echo "<strong>".$value."</strong><br /><br />"; $previousCompany = "";}}// companyif ( $key == 1 ) { $currentCompany = $value; if ( $currentCompany != $previousCompany ) { echo "<span style=\"font-weight:bold;color:#5d5d5d;\">".$value."</span><br /><br />"; }}if ( !empty($value) && $key != 0 && $key != 1) {echo "$value <br />\n";}if ( $key == 0 ) { $previousRegion = $value; }if ( $key == 1 ) { $previousCompany = $value; }if ( $key == 4 ) { echo "<br />"; }$tradeCounter ++;}}[/code]The variable names in my code do not reflect the sample data which I provided in this post. This code is just a reflection of my attempt to sort the array using foreach() and print the information how I would like it arranged. I hope this is clearer. Please let me know if you need additional details. Thanks for your response.Cheers,Fezzik Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113430 Share on other sites More sharing options...
.josh Posted October 24, 2006 Share Posted October 24, 2006 how about sorting the array or even usorting it and then looping it, dividing it by simply checking for a new value (like autobot vs. decepticon) Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113432 Share on other sites More sharing options...
Jenk Posted October 24, 2006 Share Posted October 24, 2006 You are mixing keys with values; sorting will be nigh on impossible like this, unless you have a whitelist of keys.Reorganise your array:[code]<?php$array = array( 'Autobots' => array( 'Bumblebee', 'Optimus Prime', /* etc */ ), 'Decepticons' => array( 'Galvatron', 'Starscream', /* etc */ ));?>[/code]then you can reference them as...[code]<?phpforeach ($array['Autobots'] as $autobot){ echo $autobot . ' transform!' . chr(10);}foreach ($array['Decepticons'] as $decepticon){ echo $decepticon . ' transform!' . chr(10);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113438 Share on other sites More sharing options...
fezzik Posted October 24, 2006 Author Share Posted October 24, 2006 Thanks for the responses. I'll see what I can come up with using your example. Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113482 Share on other sites More sharing options...
sasa Posted October 24, 2006 Share Posted October 24, 2006 try[code]<?php$myArray[0][0] = "Autobots";$myArray[0][1] = "Bumblebee";$myArray[0][2] = "VW Bug";$myArray[0][3] = "Yellow";$myArray[0][4] = "Smog Check";$myArray[1][0] = "Autobots";$myArray[1][1] = "Jazz";$myArray[1][2] = "Porsche";$myArray[1][3] = "White";$myArray[1][4] = "Steering Realignment";$myArray[2][0] = "Autobots";$myArray[2][1] = "Optimus Prime";$myArray[2][2] = "Diesel";$myArray[2][3] = "Red and Blue";$myArray[2][4] = "Catalytic Converter";$myArray[3][0] = "Decepticons";$myArray[3][1] = "Starscream";$myArray[3][2] = "Flying Machine";$myArray[3][3] = "Red";$myArray[3][4] = "Girly Voice";$myArray[3][0] = "Decepticons";$myArray[3][1] = "Megatron";$myArray[3][2] = "Pea Shooter";$myArray[3][3] = "Purple and Black";$myArray[3][4] = "Everything";function gr($group_key,$data_key,$aray) { foreach ($aray as $a) $b[$a[$group_key]] .= " => $a[$data_key]<br />"; foreach ($b as $c => $d) $output .= "$c<br />------------<br />$d<br />"; return $output; }print gr(0,1,$myArray);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113498 Share on other sites More sharing options...
fezzik Posted October 24, 2006 Author Share Posted October 24, 2006 Thanks sasa! This function is splendid! I've been trying to modify your function so that it lists all the $data_key's associated with the $group_key but have been unsuccessful. Is this easily accomplished with the function you provided?Cheers,Fezzik Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-113700 Share on other sites More sharing options...
sasa Posted October 25, 2006 Share Posted October 25, 2006 [code]<?php$myArray[0][0] = "Autobots";$myArray[0][1] = "Bumblebee";$myArray[0][2] = "VW Bug";$myArray[0][3] = "Yellow";$myArray[0][4] = "Smog Check";$myArray[1][0] = "Autobots";$myArray[1][1] = "Jazz";$myArray[1][2] = "Porsche";$myArray[1][3] = "White";$myArray[1][4] = "Steering Realignment";$myArray[2][0] = "Autobots";$myArray[2][1] = "Optimus Prime";$myArray[2][2] = "Diesel";$myArray[2][3] = "Red and Blue";$myArray[2][4] = "Catalytic Converter";$myArray[3][0] = "Decepticons";$myArray[3][1] = "Starscream";$myArray[3][2] = "Flying Machine";$myArray[3][3] = "Red";$myArray[3][4] = "Girly Voice";$myArray[3][0] = "Decepticons";$myArray[3][1] = "Megatron";$myArray[3][2] = "Pea Shooter";$myArray[3][3] = "Purple and Black";$myArray[3][4] = "Everything";function gr($group_key,$data_key,$aray) { //reorganize array if (!is_array($data_key)) $data_key = array($data_key); foreach ($aray as $a) { $o1 = array(); foreach ($data_key as $e) $o1[] = $a[$e]; $b[$a[$group_key]][] = $o1; } return $b; }function out($a) { //output array foreach ($a as $b => $c) { $out .= $b."\n".'<table border="3">'."\n"; foreach ($c as $d) { $out .= '<tr>'."\n"; foreach ($d as $e) $out .= "\t<td width=\"150\">$e</td>\n"; $out .= '</tr>'."\n"; } $out .= '</table>'."\n<br />\n\n"; } return $out;}$a = array(1,3,4,2);$b = gr(0,$a,$myArray);//print_r($b);$c = out($b);echo $c;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-114439 Share on other sites More sharing options...
fezzik Posted October 25, 2006 Author Share Posted October 25, 2006 Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/24883-sorting-arrays/#findComment-114464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.