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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ksort() in a 2d array

    I have a simple 2d array using brackets.
    I need to sort the data by a key.
    The manual only shows an example with a single dimension array.
    How do you do it with a 2d?
    PHP Code:

    $array
    [1]['date']
    $array[1]['name']
    $array[1]['city']
    $array[1]['state']
    $array[2]['date']
    $array[2]['name']
    $array[2]['city']
    $array[2]['state']

    //the manual gives this example

    $fruits = array("d"=>"lemon""a"=>"orange""b"=>"banana""c"=>"apple");
    ksort($fruits);
    foreach (
    $fruits as $key => $val) {
        echo 
    "$key = $val\n";

    Or would I be better off using array_multisort()?
    Please show an example of code for this
    Last edited by Suwannee_guy; 06-14-2014 at 03:29 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    All of this comes straight from the php manual

    PHP Code:
    <?php
    $people 
    = array(
    array(
    "name"=>"Bob","age"=>8,"colour"=>"red"),
    array(
    "name"=>"Greg","age"=>12,"colour"=>"blue"),
    array(
    "name"=>"Andy","age"=>5,"colour"=>"purple"));

    var_dump($people);
    echo 
    "-----------------------------------------<br>";

    $sortArray = array();

    foreach(
    $people as $person){
        foreach(
    $person as $key=>$value){
            if(!isset(
    $sortArray[$key])){
                
    $sortArray[$key] = array();
            }
            
    $sortArray[$key][] = $value;
        }
    }

    $orderby "name"//change this to whatever key you want from the array

    array_multisort($sortArray[$orderby],SORT_DESC,$people);

    var_dump($people);

    echo 
    "-----------------------------------------<br>";


    $orderby "age"//change this to whatever key you want from the array

    array_multisort($sortArray[$orderby], SORT_NATURAL,$people);

    var_dump($people);


    ?>
    So I put your info in (SORT OF) and ran the code. New code looks like this:

    PHP Code:
    <?php
    $items 
    = array(
    array(
    "date"=>"Bob","name"=>8,"city"=>"red"),
    array(
    "date"=>"Greg","name"=>12,"city"=>"blue"));

    var_dump($items);
    echo 
    "-----------------------------------------<br>";

    $sortArray = array();

    foreach(
    $items as $person){
        foreach(
    $person as $key=>$value){
            if(!isset(
    $sortArray[$key])){
                
    $sortArray[$key] = array();
            }
            
    $sortArray[$key][] = $value;
        }
    }

    $orderby "date"//change this to whatever key you want from the array

    array_multisort($sortArray[$orderby],SORT_DESC,$items);

    var_dump($items);

    echo 
    "-----------------------------------------<br>";


    $orderby "name"//change this to whatever key you want from the array

    array_multisort($sortArray[$orderby], SORT_NATURAL,$items);

    var_dump($items);


    ?>
    Last edited by sunfighter; 06-17-2014 at 09:59 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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