我有两个数组 ARRAY1:
Array(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
ARRAY2:
Array
(
[0] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[1] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[5] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[6] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
)
数组1将是数组2中的任何一个子数组,
现在我想要在数组2中第一个数组应该是数组1的那个数组,之后那些具有与数组1地址id相同的场地ID的子数组应该存在,然后是所有其他数组。 怎么做这个排序? 对不起,如果有任何英语错误我不是很好。
预期产出:
Array
(
[6] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[5] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[0] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[1] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
)
答案 0 :(得分:2)
这是一个自定义函数应该做你想做的事情:
function sortArr($haystack, $needle){
$out = array();
foreach ($haystack as $index => $arr){
if ($arr == $needle){
$out["$index"] = $arr;
unset ($haystack[$index]);
break;
}
}
foreach ($haystack as $index => $arr){
if ($arr['venue_id'] == $needle['venue_id']){
$out["$index"] = $arr;
unset($haystack[$index]);
}
}
if (is_array($haystack)){
return array_merge($out,$haystack);
}
}
以下是DEMO LINK
答案 1 :(得分:1)
您可以使用usort将其排序为示例订单。
function cmp($a, $b) {
return $b["venue_id"] - $a["venue_id"];
}
usort($array2,"cmp");
这一切都是由venue_id降序排序。因此,具有相同venue_id的所有数组将被分组,最高的venue_id位于顶部。如果你想要别的东西,你的回答有点不清楚。
编辑:我已经多考虑了这个问题,并提出了一个应该按照我认为要求的功能。它并不完美,但应该有效。
function customSort($array1,$array2) {
global $primaryVenue_id;
$primaryVenue_id = $array1["venue_id"];
function cmp($a, $b) {
global $primaryVenue_id;
if ($a["venue_id"] == $primaryVenue_id) return -1;
elseif ($b["venue_id"] == $primaryVenue_id) return 1;
else return $b["venue_id"] - $a["venue_id"];
}
usort($array2, "cmp");
unset($primaryVenue_id);
return $array2;
}
基本上,如果你调用函数customSort($ array1,$ array2),它将返回 $ array2 ,所有子数组首先包含与 $ array1 相同的venue_id,然后是id的其余降序。
答案 2 :(得分:1)
您可以取消设置第二个数组中的匹配元素,而不是使用function array_unshift($array, $element);
预先绑定数组。
$needle = array(
'id' => 50575,
'name' => 'Funbox - Pirates and Princesses',
'venue_id' => 3248
);
$stack = array(
[0] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[1] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[5] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[6] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
);
foreach ($stack as $k => $v) {
if ($v['id'] == $needle['id']) {
unset($stack[$k]);
array_unshift($stack, $needle);
break;
} else {
//echo 'No matching element in existing array.';
}
}
print_r($testArray);
输出将是:
Array
(
[0] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[1] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[5] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[6] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
)