Sorting a multi dimensional array

Discussion in 'PHP' started by abhishek198, Jul 16, 2010.

  1. #1
    Hi,
    I was trying to sort a multi dimensional array ..
    e.g

    $todo_cats[0]["TodoCat"]["name"] = "Basic Todo";

    $todo_cats[0]["TodoTask"][0]["id"] = 1;
    $todo_cats[0]["TodoTask"][0]["name"] = "Select a date";
    $todo_cats[0]["TodoTask"][0]["position"] = 2;

    $todo_cats[0]["TodoTask"][1]["id"] = 2;
    $todo_cats[0]["TodoTask"][1]["name"] = "Determine your budget";
    $todo_cats[0]["TodoTask"][1]["position"] = 1;

    i tried sorting using the code below .. which did not work ..

    foreach($todo_cats as $key =>$cat){

    foreach($cat["TodoTask"] as $key1 => $row){

    $position[$key1] = $row['position'];
    }
    array_multisort($position, SORT_ASC, $cat["TodoTask"]);
    }


    Any clue how do i sort it ... ??
     
    abhishek198, Jul 16, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How do you want to sort it?
     
    Deacalion, Jul 16, 2010 IP
  3. abhishek198

    abhishek198 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sort it on the basis of "position" key ...
     
    abhishek198, Jul 16, 2010 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
  5. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you don't mind creating a new array or won't affect your scripting you could sort the array and it's sub arrays individually while passing them into a new array.

    Do I make sense.
     
    caciocode, Jul 16, 2010 IP
  6. Oxi

    Oxi Peon

    Messages:
    78
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oxi, Jul 16, 2010 IP