开发者

Using usort to sort Arrays of Arrays with Arrays PHP

I have an array of arrays with arrays in PHP and I want to sort the top level of the array based on a field within each element, and I also want to sort an array within each element based on an element of an array of that element. (see example). I want it so that in the end, $data has first the item with the 'primary' value of 'first' then 'second'. I also want it so that within 'first' and 'second', the array under the field 'secondary' has first the item with the 'val' of 'foo' then 'bar' (for both 'first' and 'second')

$data = array (
    array (
        'myid'      => 4,
        'primary'   => 'second',
        'secondary' => array (
            array (
                'myid' => 10,
                'val'  => 'bar',
            ),
            array (
                'myid' => 8,
                'val'  => 'foo',
            ),
        ),
    ),
    array (
        'myid'      => 2,
        'primary'   => 'first',
        'secondary' => array (
            array (
                'myid' => 10,
                'val'  => 'bar',
            ),
            array (
                'myid' => 8,
                'val'  => 'foo',
            ),
        ),
    ),
);

function mysort($a, $b) {
    return $a['myid'] > $b['myid'];
}

echo "pre sort:\n";
print_r($data);

usort($data, 'mysort');

echo "post top level sort:\n";
print_r($data);

foreach ($data as $k=>$item) {
    usort($item['secondary'], 'mysort');
    echo "Second Level Item sort:\n";
    print_r($item['secondary']);
}

echo "post second level sort:\n";
print_r($data);

Why am I getting the following final output?

post second level sort:
Array
(
    [0] => Array
        (
            [myid] => 2
            [primary] => fi开发者_Go百科rst
            [secondary] => Array
                (
                    [0] => Array
                        (
                            [myid] => 10
                            [val] => bar
                        )

                    [1] => Array
                        (
                            [myid] => 8
                            [val] => foo
                        )

                )

        )

    [1] => Array
        (
            [myid] => 4
            [primary] => second
            [secondary] => Array
                (
                    [0] => Array
                        (
                            [myid] => 10
                            [val] => bar
                        )

                    [1] => Array
                        (
                            [myid] => 8
                            [val] => foo
                        )

                )

        )

)

By the way I'm using PHP 5.2.17.


Try this

foreach ($data as $k=>&$item) {
    usort($item['secondary'], 'mysort');
    echo "Second Level Item sort:\n";
    print_r($item['secondary']);
}

Why this is so?

Because by default foreach copy all vars in it.
So if you change variable in foreach you change only local variable, and after foreach is end, the variable will be se same as before foreach

When you add & - than foreach not copy variables, and using it by reference

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜