开发者

Sorting an irregular php array

开发者 https://www.devze.com 2023-03-24 03:06 出处:网络
I am trying to sort an irregular php array. It is irregular in that the structure is different than normal php arrays. I am usually able to sort arrays using usort, but the structure of this php array

I am trying to sort an irregular php array. It is irregular in that the structure is different than normal php arrays. I am usually able to sort arrays using usort, but the structure of this php array makes usort hard to use. Here's a basic structure of the array:

            Array
(
    [id] => Array
        (
            [0] => 3220604
            [1] => 5341522
            [2] => 1234568144
            [3] => 11
        )

    [firstname] => Array
        (
            [0] => Sam
            [1] => Donald
            [2] => Keesh
            [3] => Eat
        )

    [lastname] => Array
        (
            [0] => Fisher
            [1] => Duck
            [2] => Smelley
            [3] => Me
        )

)
开发者_运维知识库

I am trying to sort by ID, but of course, if I use usort, it tries to sort by the highest order arrays, like id, first, and last. How would I sort an array with this kind of structure?


You can use array_multisort to do this:

$arr = array(
    'id' => array(3220604, 5341522, 1234568144, 11),
    'firstname' => array('Sam', 'Donald', 'Keesh', 'Eat'),
    'lastname' => array('Fisher', 'Duck', 'Smelley', 'Me')
);

array_multisort($arr['id'], $arr['firstname'], $arr['lastname']);
var_dump($arr);

Here array_multisort sorts the values of the first array and orders the values of the other arrays accordingly.

But using a different data structure might also be advisable.


Ugh.

How about: ( (11, 'Sam', 'Fisher'), (2352335, 'Name', 'Last'), ...)

This way the ID is associated with the other bits of info. Currently there's no actual relationship between the different arrays, except for whatever you have in your head. They're not like the rows / columns of a table.


You should restructure your array to:

array(array('id' => 1, 'first_name' => 'David', 'last_name' => 'Smith'))

This will make your life easier. Take a look at the function from here http://www.whypad.com/posts/php-sort-multi-dimensional-array/848/:

function sortMultiArray(&$array, $key) {
    foreach($array as &$value) {
        $value['__________'] = $value[$key];
    }
    /* Note, if your functions are inside of a class, use:
        usort($array, array("My_Class", 'sort_by_dummy_key'));
    */

    usort($array, 'sort_by_dummy_key');

    foreach($array as &$value) {   // removes the dummy key from your array
        unset($value['__________']);
    }
    return $array;
}

function sort_by_dummy_key($a, $b) {
    if($a['__________'] == $b['__________']) return 0;
    if($a['__________'] < $b['__________']) return -1;
    return 1;
}


This might help you out:

http://www.php.net/manual/en/function.usort.php

This lets you specify a comparison function to sort().


If you just want to sort by IDs, you could do

asort($aryName['id']);

This will sort the IDs, but keep the their keys correct to reference the persons first and last name.

You could sort the other subarrays the same way

asort($aryName['id']);
asort($aryName['firstname']);
asort($aryName['lastname']);


Can you change the structure of the array? The way it is outlined now is poor design.

Something like:

array
(
    0 => array(
        'id'        => 3220604,
        'firstname' => 'Sam',
        'lastname'  => 'Fisher'
    ),

    1 => array(
        'id'        => 5341522,
        'firstname' => 'Donald',
        'lastname'  => 'Duck'
    ),
);

Then you could use the usort function (as specified above) to sort. And more importantly, your data will be stored in a more logical, easier to handle manner.


May be constructing the array like

$allInOneArray= array (
   "id1"  => array("firstname" => "Peter", "lastname" => "Paul"),
   "id2"  => array("firstname" => "Mary", "lastname" => "May")
   //etc
);

will make sorting more easy.


use this function before and after the clasical usort() solution

function transpose($M){
  foreach ($M as $row=>$cols) foreach ($cols as $col=>$value) $Mt[$col][$row]=$value;
  return ($Mt);           
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号