我有两个数组,
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => art
)
[1] => stdClass Object
(
[id] => 4
[title] => adsdf
)
[2] => stdClass Object
(
[id] => 2
[title] => adsdf
)
[3] => stdClass Object
(
[id] => 7
[title] => adsdf
)
)
Array
(
[2] => 2
[1] => 1
)
我想在第二个数组之后对第一个数组进行排序。在第二个数组中,键和值与第一个数组id相等。所以输出必须如下。
Array
(
[0] => stdClass Object
(
[id] => 2
[title] => adsdf
)
[1] => stdClass Object
(
[id] => 1
[title] => art
)
[2] => stdClass Object
(
[id] => 4
[title] => adsdf
)
[3] => stdClass Object
(
[id] => 7
[title] => adsdf
)
)
答案 0 :(得分:2)
您可以使用array_multisort
[Docs]:
array_multisort($arraySort, $arrayData);
使用排序顺序作为第一个数组并将数组作为第二个参数进行排序。
您可能需要在它之前构建排序数组,从您的问题来看,如果您已经拥有它,我不清楚。
如果不是,如果要将所有数据数组条目ID值放入排序数组中:
$arraySort = array();
foreach($arrayData as $key => $obj)
{
$arraySort[$key] = $obj->id;
}