我想创建一个数组,我可以随机删除 [id] 键的内容,其中它们是相同的 [parent_id]。
这是我的数组:
大批 ( [0] => 数组 ( [parent_id] => 1 [id] => 286 ) [1] => 数组 ( [parent_id] => 286 [id] => 142 ) [2] => 数组 ( [parent_id] => 1 [id] => 287 ) [3] => 数组 ( [parent_id] => 1 [id] => 288 ) [4] => 数组 ( [parent_id] => 1 [id] => 289 ) [5] => 数组 ( [parent_id] => 289 [id] => 140 ) [6] => 数组 ( [parent_id] => 289 [id] => 143 ) [7] => 数组 ( [parent_id] => 289 [id] => 242 ) )
我使用以下代码生成了它:
$parentList = array();
foreach ($list as $item)
{
$parentList[] = array("parent_id" => $item->parent_id, "id" => $item->id);
}
我不知道如何将数组转换为:
大批 ( [0] => 数组 ( [parent_id] => 1 [id] => 数组 (286, 287, 288, 289) ) [1] => 数组 ( [parent_id] => 286 [id] => 数组 (142) ) [2] => 数组 ( [parent_id] => 289 [id] => 数组 (140, 143, 242) ) )
之后,我希望在每个 [id] 数组中随机保留每个父 ID 的一个 ID。
我怎样才能做到这一点?