我有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
)
答案 0 :(得分:2)
$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);
这是重建,可能不是排序
答案 1 :(得分:1)
另外,你可以使用这个技巧:
$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
。
答案 2 :(得分:0)
请参阅以下链接: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] ;
}