Jump to content

sortign a two dimensional array


bdee1

Recommended Posts

i have a 2 dimensional array which is basically an array where each element contains an associative array. 

 

one of the keys of the associative array is id which contains a numeric value.

 

how woudl i go about sorting the root array in the order of the id element from the associative arrays?

 

 

Link to comment
https://forums.phpfreaks.com/topic/142538-sortign-a-two-dimensional-array/
Share on other sites

to Sort an array and maintain index association

use asort()

 

EDIT:

if you want to sort by first item try this

 

<?php
aksort($array);
print_r($array);

function aksort(&$array) {
  asort($array);
  $vals = array_count_values($array);
    $i = 0;
    foreach ($vals AS $val=>$num) {
        $first = array_splice($array,0,$i);
        $tmp = array_splice($array,0,$num);
        ksort($tmp);
        $array = array_merge($first,$tmp,$array);
        unset($tmp);
        $i = $i+$num;
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.