我有以下要重新安排的数组
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
)
答案 0 :(得分:-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&#34 ;在array_push之前)