1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Multi Dimensional php sort array

Discussion in 'PHP' started by Silver89, Nov 28, 2009.

  1. #1
    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?
     
    Silver89, Nov 28, 2009 IP
  2. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try using the PREG_SET_ORDER flag in your preg_match(_all), it made my life a lot easier.
     
    szalinski, Nov 28, 2009 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    That's a different script on another site, this is just for arranging array content.
     
    Silver89, Nov 28, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    JAY6390, Nov 28, 2009 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    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:
     
    shallowink, Nov 28, 2009 IP
    Silver89 likes this.
  6. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #6
    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):
    :)
     
    wing, Nov 28, 2009 IP
    Silver89 likes this.
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That code is exactly the same as mine other than you've changed the variable names and changed the implode to a simple echo :rolleyes:
     
    JAY6390, Nov 28, 2009 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    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.
     
    shallowink, Nov 28, 2009 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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];
    :rolleyes:
     
    JAY6390, Nov 28, 2009 IP
  10. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #10
    not trying to be a jerk but :
    outputs nothing. so did I miss something in copying your code over?
     
    shallowink, Nov 28, 2009 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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:
     
    JAY6390, Nov 28, 2009 IP
  12. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #12
    forgot to say I also know you weren't trying to be a jerk :)
     
    JAY6390, Nov 28, 2009 IP
  13. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #13
    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.
     
    shallowink, Nov 28, 2009 IP
  14. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #14
    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...
     
    markowe, Nov 29, 2009 IP
    Silver89 likes this.
  15. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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
     
    JAY6390, Nov 29, 2009 IP
  16. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #16
    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" :)
     
    wing, Nov 29, 2009 IP
  17. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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
     
    JAY6390, Nov 29, 2009 IP
  18. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #18
    
    $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:
     
    Last edited: Nov 30, 2009
    cornetofreak, Nov 30, 2009 IP
  19. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #19
    That will output the h2 twice for football and other. The OP wants one H2 with the two p's below it
     
    JAY6390, Nov 30, 2009 IP