PHP多维数组排序将一个元素值保持在第一位置

时间:2019-02-22 06:35:42

标签: php arrays multidimensional-array

我有一个名为$ newArray的多维数组,具有以下值:

$newArray = 
   (
    [0] => Array
        (
            [CountryID] => 24
            [val] => 1
            [PostID] => 1
            [id] => 0
        )

    [1] => Array
        (
            [CountryID] => 355
            [val] => 0.1
            [PostID] => 2
            [id] => 1
        )

    [2] => Array
        (
            [CountryID] => 241
            [val] => 0.3
            [PostID] => 3
            [id] => 2
        )

    [3] => Array
        (
            [CountryID] => 135
            [val] => 0.02
            [PostID] => 4
            [id] => 3
        )

    [4] => Array
        (
            [CountryID] => 24
            [val] => 0.003
            [PostID] => 5
            [id] => 4
        )

    [5] => Array
        (
            [CountryID] => 355
            [val] => 5
            [PostID] => 6
            [id] => 5
        )

    [6] => Array
        (
            [CountryID] => 355
            [val] => 4
            [PostID] => 23
            [id] => 6
        )

    [7] => Array
        (
            [CountryID] => 24
            [val] => 2
            [PostID] => 1
            [id] => 7
        )

    [8] => 
)

仅在过滤掉[CountryID]的一个元素值“ 24”(当然是[val]的一个元素值“ 24”之后,我才想在DESC中对[CountryID]进行排序,[val]在DESC中进行排序),因此结果应在以下位置:

$newArray = 
    (
        [1] => Array
            (
                [CountryID] => 24
                [val] => 2
                [PostID] => 1
                [id] => 7
            )

        [2] => Array
            (
                [CountryID] => 24
                [val] => 1
                [PostID] => 1
                [id] => 0
            )       

        [3] => Array
            (
                [CountryID] => 24
                [val] => 0.003
                [PostID] => 5
                [id] => 4
            )

        [4] => Array
            (
                [CountryID] => 355
                [val] => 5
                [PostID] => 6
                [id] => 5
            )

        [5] => Array
            (
                [CountryID] => 355
                [val] => 4
                [PostID] => 23
                [id] => 6
            )

        [6] => Array
            (
                [CountryID] => 355
                [val] => 0.1
                [PostID] => 2
                [id] => 1
            )

        [7] => Array
            (
                [CountryID] => 241
                [val] => 0.3
                [PostID] => 3
                [id] => 2
            )

        [8] => Array
            (
                [CountryID] => 135
                [val] => 0.02
                [PostID] => 4
                [id] => 3
            )   

    )

我应该在此处使用什么函数对具有上述顺序和条件的多维数组进行排序?

我仍在学习php数组,对此知识有限。

2 个答案:

答案 0 :(得分:1)

您可以稍后提取所需的值,并对数组进行过滤以避免警告。

$inputSearch = 24;
$k = array_keys(array_column($newArray, 'CountryID'), 24);    
foreach($k as $v){
    $temp[] = $newArray[$v];    
    unset($newArray[$v]);
}
array_multisort(array_column($temp, 'val'), SORT_DESC, $temp);
$newArray = array_filter($newArray);
array_multisort(array_column($newArray, 'CountryID'), SORT_DESC,array_column($newArray, 'val'),      SORT_DESC,$newArray);
$newArray = array_merge($temp , $newArray);                
print_r($newArray);

array_column-从输入数组的单个列中返回值
array_filter-使用回调函数过滤数组的元素
array_search —在数组中搜索给定值,如果成功,则返回第一个对应的键
array_multisort —对多维或多维数组进行排序
array_unshift —在数组的开头添加一个或多个元素
array_keys —返回数组的所有键或键的子集

Demo

输出

Array
(
    [0] => Array
        (
            [CountryID] => 24
            [val] => 2
            [PostID] => 1
            [id] => 7
        )

    [1] => Array
        (
            [CountryID] => 24
            [val] => 1
            [PostID] => 1
            [id] => 0
        )

    [2] => Array
        (
            [CountryID] => 24
            [val] => 0.003
            [PostID] => 5
            [id] => 4
        )

    [3] => Array
        (
            [CountryID] => 355
            [val] => 5
            [PostID] => 6
            [id] => 5
        )

    [4] => Array
        (
            [CountryID] => 355
            [val] => 4
            [PostID] => 23
            [id] => 6
        )

    [5] => Array
        (
            [CountryID] => 355
            [val] => 0.1
            [PostID] => 2
            [id] => 1
        )

    [6] => Array
        (
            [CountryID] => 241
            [val] => 0.3
            [PostID] => 3
            [id] => 2
        )

    [7] => Array
        (
            [CountryID] => 135
            [val] => 0.02
            [PostID] => 4
            [id] => 3
        )

)

答案 1 :(得分:0)

使用usort可以解决您的问题。示例:

usort($newArray, function($a, $b) {
    if ($a['CountryID'] == 135 OR $b['CountryID'] == 135) return 1;
    $retval = $b['CountryID'] - $a['CountryID'];
    if ($retval == 0) {
        $retval = $b['val'] - $a['val'];
    }
    return $retval;
});

echo '<pre>', print_r($newArray);