我有下面的数组,我想重新排列
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
我想让它看起来像这样
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
这段代码应该可以工作了:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
您应该首先创建一个空数组来存储新数据。然后,在foreach中,你应该使用array_push,但是如果你想要放置数据的子数组不是数组,你应该首先声明它(这就是为什么"if"在array_push之前)