Hi, I'm trying to sort the following multi function array so it outputs the data in this format: The raw array is as follows: Array ( [1] => Array ( [0] => Other [1] => Qatar Classic ) [2] => Array ( [0] => Other [1] => SA Horse Racing Live ) [3] => Array ( [0] => Tennis [1] => ATP Challenger Bratislava ) [4] => Array ( [0] => Basketball [1] => Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar ) [5] => Array ( [0] => Football [1] => HB Koege vs. FC Nordsjaelland ) [6] => Array ( [0] => Football [1] => Tavriya Simferopol vs. Zakarpattya Uzgorod ) ) PHP: I'm completely stumped at the moment as how to show part of an array uniquelly but show the results that also have that unique value?
I think you'd actually need to loop over the arrays like so $tmp = array(); foreach($your_array as $v) $tmp[$v[0]] = $v[1]; ksort($tmp); foreach($tmp as $k=>$v) { echo '<h2>'.$k.'</h2>'; echo implode('<br />', $v); } PHP: The first loop groups the items into categories It then sorts the keys alphabetically Then loops through and outputs them I set the output to have a <br /> between the items. Not sure if that is what you wanted to do or not The second displays them
This is ugly, but it does what you want: <?php $fm = Array ( Array ('Other' => 'Qatar Classic' ), Array( 'Other'=> 'SA Horse Racing Live' ), Array ( 'Tennis' => 'ATP Challenger Bratislava' ), Array ( 'Basketball' => 'Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar' ), Array ( 'Football' => 'HB Koege vs. FC Nordsjaelland' ), Array ( 'Football' => 'Tavriya Simferopol vs. Zakarpattya Uzgorod' ) ); foreach($fm as $key=>$val) { foreach($val as $k1=>$v2) { $head[] = $k1; } } $headers = array_unique($head); asort($headers); foreach($headers as $key=>$h2) { echo "<h2>$h2</h2>"; echo "<ul>"; foreach($fm as $key=>$val) { foreach($val as $k1=>$v2) { if($k1 == $h2) { echo "<li>$v2</li>"; } } } echo "</ul>"; } ?> PHP: output:
Here is another one // your array with events $aMultiDim = array ( 1 => array (0 => 'Other', 1 => 'Qatar Classic' ), 2 => array (0 => 'Other', 1 => 'SA Horse Racing Live' ), 3 => array (0 => 'Tennis', 1 => 'ATP Challenger Bratislava' ), 4 => array (0 => 'Basketball', 1 => 'Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar' ), 5 => array (0 => 'Football', 1 => 'HB Koege vs. FC Nordsjaelland'), 6 => array (0 => 'Football', 1 => 'Tavriya Simferopol vs. Zakarpattya Uzgorod') ); // group the events in $aKeys $aKeys = array(); foreach ($aMultiDim as $iIdx => $aArr) { $aKeys[$aArr[0]][] = $aArr[1]; } //sort them ksort($aKeys); // show them foreach ($aKeys as $sKey => $aEvents) { echo('<h2>'.$sKey.'</h2>'); foreach($aEvents as $sEvent) { echo($sEvent . '<br />'); } } PHP: output: <h2>Basketball</h2>Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar<br /><h2>Football</h2>HB Koege vs. FC Nordsjaelland<br />Tavriya Simferopol vs. Zakarpattya Uzgorod<br /><h2>Other</h2>Qatar Classic<br />SA Horse Racing Live<br /><h2>Tennis</h2>ATP Challenger Bratislava<br /> Code (markup):
That code is exactly the same as mine other than you've changed the variable names and changed the implode to a simple echo
yours didn't output all the elements (Qatar Classic is missing from Other): Basketball Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar Football Tavriya Simferopol vs. Zakarpattya Uzgorod Other SA Horse Racing Live Tennis ATP Challenger Bratislava Code (markup): so its your code plus the foreach he added.
Well to be honest I didn't test it, but yeah the only thing was this $tmp[$v[0]] = $v[1]; should have been $tmp[$v[0]][] = $v[1];
No I know, I tried the array you posted with it to begin with and realised why your array wouldnt work with my code The original post doesn't have an associative array, it has two elements (0 and 1), eg [0] => Other [1] => Qatar Classic The array I've put below shows an actual array that is identical to the OP's one $your_array = array( array( 'Other', 'Qatar Classic' ), array( 'Other', 'SA Horse Racing Live' ), array( 'Tennis', 'ATP Challenger Bratislava' ), array( 'Basketball', 'Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar' ), array( 'Football', 'HB Koege vs. FC Nordsjaelland' ), array( 'Football', 'Tavriya Simferopol vs. Zakarpattya Uzgorod' ) ); $tmp = array(); foreach ($your_array as $v) $tmp[$v[0]][] = $v[1]; ksort($tmp); foreach ($tmp as $k => $v) { echo '<h2>'.$k.'</h2>'; echo implode('<br />', $v); } PHP: And the output is <h2>Basketball</h2>Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar<h2>Football</h2>HB Koege vs. FC Nordsjaelland<br />Tavriya Simferopol vs. Zakarpattya Uzgorod<h2>Other</h2>Qatar Classic<br />SA Horse Racing Live<h2>Tennis</h2>ATP Challenger Bratislava HTML:
ah cool, I see what's going on. I wouldn't do my array that way though, guess I'll go toy with it some more.
It's weird that PHP still doesn't include very good support for sorting arrays. I guess that's something that will come along with new versions of PHP, but I have had to use similar solutions to the above in the meantime. Maybe in some cases, if server overhead is not a big issue and maybe if you have a lot of data, you could insert it all in a database and then select it out again with ORDER BY - could be a better solution in some cases. One of these days I'll try both and benchmark them to see which is quicker...
It's not the best no, but there are plenty of functions that will give you workarounds. array_map() is one of the best I've found and yet it's so rarely used which is a shame
Hey JAY6390, I'm really sorry if I offended you in some way. I Just felt like giving the OP an example on a solution to his problem. Please accept my sincere apologies. Maybe I can be "the jerk of the thread"
haha not a problem my friend, I just wondered why you put what I thought was the same code No harm done, and if anything it made me re-check my code and find the problem which was that it was missing the [] for the tmp array
$fm = Array ( Array ('Other' => 'Qatar Classic' ), Array( 'Other'=> 'SA Horse Racing Live' ), Array ( 'Tennis' => 'ATP Challenger Bratislava' ), Array ( 'Basketball' => 'Spartak St Petersburg vs. Lokomotiv Kuban Krasnodar' ), Array ( 'Football' => 'HB Koege vs. FC Nordsjaelland' ), Array ( 'Football' => 'Tavriya Simferopol vs. Zakarpattya Uzgorod' ) ); arsort($fm); foreach($fm as $object){ //Now loop your objects foreach($object as $h2 => $p){ echo sprintf("<h2>%s</h2><p>%s</p>",$h2,$p); } } PHP: