【问题标题】:Sort multiple according to the keys of first array [closed]根据第一个数组的键对多个进行排序[关闭]
【发布时间】:2013-12-18 06:26:50
【问题描述】:

我有 3 个不同的数组,它们的键相同但值不同

Array 1
(
[product_category_39] => Living Room
[product_category_40] => Dining Room
[product_category_38] => Bedroom
[product_category_44] => Kids Room
[product_category_43] => Home Office
[product_category_42] => Decor
[product_category_11] => Furnishings
[product_category_41] => Kitchen & Table Top
[product_category_45] => Bath
)

Array 2
(
[product_category_40] => std Object()
[product_category_39] => std Object()
[product_category_45] => std Object()
[product_category_38] => std Object()
[product_category_11] => std Object()
[product_category_42] => std Object()
[product_category_41] => std Object()
[product_category_43] => std Object()
[product_category_44] => std Object()
)

Array 3
(
[product_category_44] => val6
[product_category_39] => xyz
[product_category_42] => data5
[product_category_41] => pqr
[product_category_45] => val2
[product_category_11] => lmn
[product_category_38] => data12
[product_category_43] => abc
[product_category_40] => val 1
)

我想根据我的第一个数组的键对这 3 个数组进行排序。键的顺序应与第一个数组中的顺序相同。有什么可能的方法来做到这一点。

排序后应该是这样的

Array 1
(
[product_category_39] => Living Room
[product_category_40] => Dining Room
[product_category_38] => Bedroom
[product_category_44] => Kids Room
[product_category_43] => Home Office
[product_category_42] => Decor
[product_category_11] => Furnishings
[product_category_41] => Kitchen & Table Top
[product_category_45] => Bath
) 


Array 2
(
[product_category_39] => std Object()
[product_category_40] => std Object()
[product_category_38] => std Object()
[product_category_44] => std Object()
[product_category_43] => std Object()
[product_category_42] => std Object()
[product_category_11] => std Object()
[product_category_41] => std Object()
[product_category_45] => std Object()
)

Array 3
(
[product_category_39] => xyz
[product_category_40] => val 1
[product_category_38] => data12
[product_category_44] => val6
[product_category_43] => abc
[product_category_42] => data5
[product_category_11] => lmn
[product_category_41] => pqr
[product_category_45] => val2
)

【问题讨论】:

  • 你有什么尝试吗?我们可以从那里开始为您提供帮助。
  • 我对此不太了解。我尝试了 ksort(),asort() 但我的键正在改变。然后我尝试了 array_multisort(array1,array2) 但这也没有按预期工作。
  • 当我需要这种东西时,我总能找到一个排序的例子搜索Stack Overflow档案。简单、复杂、多维,应有尽有……真的,搜索一下,您会发现这里已经发布了解决方案。

标签: php arrays


【解决方案1】:
$arr1 = array(
    'b'=>1,
    'a'=>2,
    'c'=>3
);

$arr2 = array(
    'a'=>4,
    'b'=>5,
    'c'=>6
);

function sort_by_set($arr1,$arr2){
    foreach($arr1 as $key=>$value){
        $arr_new[$key] = $arr2[$key];
    }
    return $arr_new;
}

$arr2_new = sort_by_set($arr1,$arr2);
print_r($arr2_new);

这是重建,可能不是排序

【讨论】:

  • 我同意。这是重建;但除此之外,没有什么能成为我。好又容易。
  • 谢谢。有效。但它正在增加一个循环。
【解决方案2】:

另外,你可以使用这个技巧:

$first = array(
    'one' => 1,
    'three' => 3,
    'two' => 2,
);
$second = array(
    'two' => 'two',
    'one' => 'one',
    'three' => 'three'
);
uksort($second, function ($a, $b) use ($first) {
    foreach ($first as $key => $v) {
        if ($a == $key) {
            if ($b == $key) {
                return 0;
            }
            return -1;
        }
        if ($b == $key)
            return 1;
    }
    return 1;
});
var_dump($second);

在你的情况下,有几个数组,你可以带走回调函数并将$first用作global

【讨论】:

    【解决方案3】:

    请参考以下链接:Sort array using another array keys

    它回答了您的问题,或者至少可以让您开始了解如何处理它。祝你好运!

    $arr1 = array(
        'a' => '42', 
        'b' => '551',
        'c' => '512',
        'd' => 'gge',
    ) ;
    
    
    $arr2 = array(
        'd' => 'ordered',
        'b' => 'is',
        'c' => 'now',
        'a' => 'this', 
    ) ;
    
    $arr2ordered = array() ;
    
    foreach (array_keys($arr1) as $key) {
        $arr2ordered[$key] = $arr2[$key] ;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-19
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多