PHP - 按SubArray值排序数组通过维护原始Array键

时间:2016-09-20 08:51:41

标签: php sorting multidimensional-array

我想使用子数组值(" Name")对数组进行排序,但保留其原始数组键。

默认数组:

Array (
    [251] => Array
        (
            [color] => 
            [name] => 8
            [nbr] => 1
            [url_name] => taille-8
            [meta_title] => 
        )

    [323] => Array
        (
            [color] => 
            [name] => 7
            [nbr] => 2
            [url_name] => taille-7
            [meta_title] => 
        )

    [127] => Array
        (
            [color] => 
            [name] => 34
            [nbr] => 2
            [url_name] => taille-34
            [meta_title] => 
        )
);

通过使用array_multisort,我可以获得以下数组:

Array(
    [0] => Array
        (
            [color] => 
            [name] => 7
            [nbr] => 2
            [url_name] => taille-7
            [meta_title] => 
        )

    [1] => Array
        (
            [color] => 
            [name] => 8
            [nbr] => 1
            [url_name] => taille-8
            [meta_title] => 
        )

    [2] => Array
        (
            [color] => 
            [name] => 34
            [nbr] => 2
            [url_name] => taille-34
            [meta_title] => 
        )
);

但我需要的是,

Array(
    [323] => Array
        (
            [color] => 
            [name] => 7
            [nbr] => 2
            [url_name] => taille-7
            [meta_title] => 
        )

    [251] => Array
        (
            [color] => 
            [name] => 8
            [nbr] => 1
            [url_name] => taille-8
            [meta_title] => 
        )

    [127] => Array
        (
            [color] => 
            [name] => 34
            [nbr] => 2
            [url_name] => taille-34
            [meta_title] => 
        )
);

感谢adv:)

4 个答案:

答案 0 :(得分:1)

from openpyxl import Workbook file_name="D:\\a.txt" content={} with open(file_name) as f: for line in f: (key,value)=line.split(":") content[key]=value wb=Workbook() ws=wb.active r = 2 for item in content: ws.cell(row=r, column=3).value = item ws.cell(row=r, column=4).value = content[item] r += 1 wb.save("D:\\Reports.xlsx") 成为您的阵列:

$arr

您可能错过了将数组与给定键组合的最后一步。

答案 1 :(得分:1)

$arr假设您的数组包含数字键并使用array_multisort对其进行排序。

array_multisort将返回已排序的数组。
array_combine会将您的原始密钥与已排序的数组合并。

使用:

$result = array_sort_by_column_preserve_keys($arr);
echo '<pre>';print_r($result);echo '</pre>';

方法:

function array_sort_by_column_preserve_keys($arr) {
    $ar2 = [];
    foreach($arr as $key => $sub) {
        $ar2[ $key ] = $sub;
    }
    $keys = array_keys($arr);
    array_multisort($ar2, SORT_ASC, SORT_NUMERIC, $arr, $keys);
    return $result = array_combine($keys, $arr);
}

答案 2 :(得分:1)

我会选择uasort,对我来说看起来更简单:

// $arr is your Array
uasort($arr, function ($a, $b) {
  return $a['name'] - $b['name'];
});

以下是一个示例:http://sandbox.onlinephpfunctions.com/code/a9f2d1e9702834b3a35206125429739222770301

答案 3 :(得分:0)

请试试这个,

$array=array(
    "251" => array(
        "color" => "",
        "name" => 8,
        "nbr" => 1,
        "url_name" => "taille-8",
        "meta_title" => ""),

    "323" => array(
        "color" => "",
        "name" => 7,
        "nbr" => 2,
        "url_name" => "taille-7",
        "meta_title" => ""),
    "127" => array(
        "color" => "",
        "name" => 34,
        "nbr" => 2,
        "url_name" => "taille-34",
        "meta_title" => ""),
    );
function swapArray( &$arr,$firstPos,$secondPos){
    //echo PHP_EOL."swap: ".$firstPos.", ".$secondPos.PHP_EOL;
    foreach($arr[$firstPos] as $k=>$v){
        //echo PHP_EOL.$k." => ".$v.PHP_EOL;
        $tmp=$v;
        $arr[$firstPos][$k]=$arr[$secondPos][$k];
        $arr[$secondPos][$k]=$tmp;
    }
}
    var_dump($array);

    $keys=array(); // store all key values
    $num=count($array);

    foreach($array as $key=>$tmpArray)
        $keys[]=$key;

    //var_dump($keys);

    for($i=0; $i<$num; $i++){
        for($j=$i+1; $j<$num; $j++){
            if($array[$keys[$i]]["name"]>$array[$keys[$j]]["name"]){
                swapArray($array,$keys[$i],$keys[$j]);
            }
        }
    }


    var_dump($array);