Home > Article > Backend Development > 数组根据val值的相同性组成多个数组
原始数组类似
$arr = array( array( 'id' => '1', 'name' => 'a', 'project' => '1', ), array( 'id' => '2', 'name' => 'b', 'project' => '3', ), array( 'id' => '3', 'name' => 'c', 'project' => '2', ), array( 'id' => '4', 'name' => 'd', 'project' => '1', ), );
$arr1 = array( array( 'id' => '1', 'name' => 'a', 'project' => '1', ), array( 'id' => '4', 'name' => 'd', 'project' => '1', ), ); $arr2 = array( array( 'id' => '2', 'name' => 'b', 'project' => '3', ), ); $arr3 = array( array( 'id' => '3', 'name' => 'c', 'project' => '2', ), );
$arr = array( array( 'id' => '1', 'name' => 'a', 'project' => '1', ), array( 'id' => '2', 'name' => 'b', 'project' => '3', ), array( 'id' => '3', 'name' => 'c', 'project' => '2', ), array( 'id' => '4', 'name' => 'd', 'project' => '1', ), );foreach($arr as $v) $r['arr' . $v['project']][] = $v;extract($r);print_r($arr1);
Array( [0] => Array ( [id] => 1 [name] => a [project] => 1 ) [1] => Array ( [id] => 4 [name] => d [project] => 1 ))其他自己验证
extract()一般什么时候用
谢谢了,第一次知道$r['arr' . $v['project']][] = $v; 还可以让数组自己找的对应位置,其实这一句就到达我想要的结果了,我那种方法自己都觉得累
extract()之前看了下,大致就是把val直接给到$+key名的变量中