Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Question Sorting an array of arrays by the value of a certain key in side an array in the main

    This may sound confusing so lets try to make it simpler with layout :P

    Lets say i have an array like this:
    PHP Code:
    $myBigArray = array(
    [
    0] => array('name' => 'Bob''city' => 'san francisco''state' => 'california'),
    [
    1] => array('name' => 'Joe''city' => 'new york city''state' => 'new york'),
    [
    2] => array('name' => 'Jim''city' => 'westfield''state' => 'new york'),
    [
    3] => array('name' => 'Mary''city' => 'carson city''state' => 'california'),
    [
    4] => array('name' => 'Sam''city' => 'paterson''state' => 'new york'),
    ); 
    Now if you were to print_r this out, it would be something like this:
    Code:
    array(
    	[0] => array(
    		name => Bob
    		city => san francisco
    		state => california
    	)
    	[1] => array(
    		name => Joe
    		city => new york city
    		state => new york
    	)
    	[2] => array(
    		name => Jim
    		city => westfield
    		state => new york
    	)
    	[3] => array(
    		name => Mary
    		city => carson city
    		state => california
    	)
    	[4] => array(
    		name => Sam
    		city => paterson
    		state => new york
    	)
    )
    My question is, how can i resort this based on a certain key in this? Say we want to group them by the states alphabetically. The result I want is this:
    Code:
    array(
    	[0] => array(
    		name => Bob
    		city => san francisco
    		state => california
    	)
    	[1] => array(
    		name => Mary
    		city => carson city
    		state => california
    	)
    	[2] => array(
    		name => Joe
    		city => new york city
    		state => new york
    	)
    	[3] => array(
    		name => Jim
    		city => westfield
    		state => new york
    	)
    	[4] => array(
    		name => Sam
    		city => paterson
    		state => new york
    	)
    )
    Yes i want the array numbers to change also. I tried various ways of asort , ksort, etc but didnt seem to do it right unless i did it wrong.
    Last edited by Dubz; 01-15-2012 at 11:12 PM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You are sorting a multidimensional array so did you try using the multisort function?
    http://www.php.net/manual/en/functio...-multisort.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:
    <?php

    $myBigArray 
    = array(
        array(
    'name' => 'Bob''city' => 'san francisco''state' => 'california'),
        array(
    'name' => 'Joe''city' => 'new york city''state' => 'new york'),
        array(
    'name' => 'Jim''city' => 'westfield''state' => 'new york'),
        array(
    'name' => 'Mary''city' => 'carson city''state' => 'california'),
        array(
    'name' => 'Sam''city' => 'paterson''state' => 'new york'),
    );
    var_dump($myBigArray);

    // Grab list of states to sort by
    foreach($myBigArray as $record) {
        
    $sortColumn[] = $record['state'];
    }

    // Sort by states
    array_multisort($sortColumnSORT_ASC$myBigArray);
    var_dump($myBigArray);
    ?>
    Results:
    PHP Code:
    array
      
    => 
        array
          
    'name' => string 'Bob' (length=3)
          
    'city' => string 'san francisco' (length=13)
          
    'state' => string 'california' (length=10)
      
    => 
        array
          
    'name' => string 'Joe' (length=3)
          
    'city' => string 'new york city' (length=13)
          
    'state' => string 'new york' (length=8)
      
    => 
        array
          
    'name' => string 'Jim' (length=3)
          
    'city' => string 'westfield' (length=9)
          
    'state' => string 'new york' (length=8)
      
    => 
        array
          
    'name' => string 'Mary' (length=4)
          
    'city' => string 'carson city' (length=11)
          
    'state' => string 'california' (length=10)
      
    => 
        array
          
    'name' => string 'Sam' (length=3)
          
    'city' => string 'paterson' (length=8)
          
    'state' => string 'new york' (length=8)

    array
      
    => 
        array
          
    'name' => string 'Bob' (length=3)
          
    'city' => string 'san francisco' (length=13)
          
    'state' => string 'california' (length=10)
      
    => 
        array
          
    'name' => string 'Mary' (length=4)
          
    'city' => string 'carson city' (length=11)
          
    'state' => string 'california' (length=10)
      
    => 
        array
          
    'name' => string 'Jim' (length=3)
          
    'city' => string 'westfield' (length=9)
          
    'state' => string 'new york' (length=8)
      
    => 
        array
          
    'name' => string 'Joe' (length=3)
          
    'city' => string 'new york city' (length=13)
          
    'state' => string 'new york' (length=8)
      
    => 
        array
          
    'name' => string 'Sam' (length=3)
          
    'city' => string 'paterson' (length=8)
          
    'state' => string 'new york' (length=8
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Spookster View Post
    PHP Code:
    // Grab list of states to sort by
    foreach($myBigArray as $record) {
        
    $sortColumn[] = $record['state'];
    }

    // Sort by states
    array_multisort($sortColumnSORT_ASC$myBigArray);
    ?> 
    Just what I needed. Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •