[[0]=>[adminid =>1,group =>小组1],[1]=>[adminid =>2,group =>小组2],[2]=>[adminid =>2,group =>小组3]];如何让adminid 相同的放进同一个下标?拼成这样的:[[0]=>[adminid =>1,group =>小组1],[1]=>[[0]=>[adminid =>2,group =>小组2],[1]=>[adminid =>2,group =>小组3]]];
好好学习,天天向上!!!
PHP two-dimensional array splicing problem? -PHP Chinese website Q&A-PHP two-dimensional array splicing problem? -PHP Chinese website Q&A
Let’s take a look and learn.
看你的数组吧,如果数组不是很大,循环就可以了。但是数组太大,会消耗内存,需要想一些特别的办法。
public function actionTest(){ $list = []; $arr = [ ['adminid' => 1, 'group' => '小组1'], ['adminid' => 2, 'group' => '小组2'], ['adminid' => 2, 'group' => '小组3'], ]; foreach ($arr as $value) { if (isset($list[$value['adminid']])) { $list[$value['adminid']][] = $value; continue; } $list[$value['adminid']][] = $value; } unset($arr); print_r($list); }
PHP two-dimensional array splicing problem? -PHP Chinese website Q&A-PHP two-dimensional array splicing problem? -PHP Chinese website Q&A
Let’s take a look and learn.
看你的数组吧,如果数组不是很大,循环就可以了。但是数组太大,会消耗内存,需要想一些特别的办法。