简体   繁体   English

PHP排序多维数组失败

[英]PHP Sorting Multidimensional Array Failed

I have this array : 我有这个数组:

Array
(
    [0] => Array
        (
            [id] => 83
            [value] => Figures
        )

    [1] => Array
        (
            [id] => 85
            [value] => Toys
        )

    [2] => Array
        (
            [id] => 36
            [value] => Nintendo Switch
        )

)

and I have this code to sort that array based on id : 而且我有这段代码根据id对那个数组进行排序:

function cmp($a, $b) {
    return strcmp($a->id, $b->id);
}

while ($row = $result->fetch_assoc()) {

    $category = json_decode($row['product_cat'], true);

    usort($category, "cmp");

    echo '<pre>';
    print_r($category);
    echo '</pre>';
}

the result is not working as I expected, because id=85 placed before id=83 : 结果无法正常工作,因为id=85放在id=83之前:

Array
(
    [0] => Array
        (
            [id] => 36
            [value] => Nintendo Switch
        )

    [1] => Array
        (
            [id] => 85
            [value] => Toys
        )

    [2] => Array
        (
            [id] => 83
            [value] => Figures
        )

)

why PHP successfully placed the id=36 as first value of array, but failed to sort id=85 and id=83 为什么PHP成功将id=36放置为数组的第一个值,但未能对id=85id=83进行排序

thank you. 谢谢。

change 更改

return strcmp($a->id, $b->id);

to

return strcmp($a['id'], $b['id']);

You can use like this 你可以这样使用

$mylist = array(array("id"=>83,"value"=>"Figures"),array("id"=>85,"value"=>"Toys"),array("id"=>36,"value"=>"Nintendo Switch"));

echo "<pre>";

$sort = array();
foreach($mylist as $k=>$v) {
    $sort['id'][$k] = $v['id'];
    $sort['value'][$k] = $v['value'];
}
# sort by event_type desc and then title asc
array_multisort($sort['id'], SORT_ASC, $sort['value'], SORT_ASC,$mylist);

print_r($mylist);

And get output like below 并获得如下输出

Array
(
    [0] => Array
        (
            [id] => 36
            [value] => Nintendo Switch
        )

    [1] => Array
        (
            [id] => 83
            [value] => Figures
        )

    [2] => Array
        (
            [id] => 85
            [value] => Toys
        )

)
$category = array ([
        'id' => 36,
        'value' => 'Nintendo Switch'
], [
        'id' => 85,
        'value' => 'Toys'
], [
        'id' => 83,
        'value' => 'Figures'
]);

$sortArry = [];
foreach ($category as $c) {
    $sortArry[$c['id']] = $c;
}

echo '<pre>';
print_r($sortArry);
array_multisort($sortArry);
print_r($sortArry);
exit;

Place id as key in your array and then use multisort. 将id作为键放在您的数组中,然后使用多排序。 It will work. 它会工作。

It is just a one liner 这只是一个班轮

array_multisort( array_column($yourArray, "id"), SORT_ASC, $yourArray );

You can find it here as well: http://php.net/manual/en/function.array-multisort.php 您也可以在这里找到它: http : //php.net/manual/en/function.array-multisort.php

search for "array_column" on that manual page. 在该手册页上搜索“ array_column”。

I used this to test: 我用它来测试:

$yourArray = array (
"0" => Array
    (
        "id" => 83,
        "value" => "Figures"
    ),
"1" => Array
    (
        "id" => 85,
        "value" => "Toys"
    ),
"2" => Array
    (
        "id" => 36,
        "value" => "Nintendo Switch"
    )
);

array_multisort( array_column($yourArray, "id"), SORT_ASC, $yourArray );
print_r($yourArray);

And the result is this: 结果是这样的:

Array ( [0] => Array ( [id] => 36 [value] => Nintendo Switch ) 
        [1] => Array ( [id] => 83 [value] => Figures ) 
        [2] => Array ( [id] => 85 [value] => Toys ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM