数组根据val值的相同性组成多个数组

WBOY
Release: 2016-06-20 12:26:54
Original
1123 people have browsed it

原始数组类似

$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',            ),        );
Copy after login

我想根据project的相同性组成多个数组,如下面结果
$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',            ),        );
Copy after login



求帮助了呀,我有个思路,想让project顺序排序,然后再循环判断下一个val[project]是否与 上一个相同,不同就组成新数组。
但是想问问 还有其他效率的方法没


回复讨论(解决方案)

$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);
Copy after login
Array(    [0] => Array        (            [id] => 1            [name] => a            [project] => 1        )    [1] => Array        (            [id] => 4            [name] => d            [project] => 1        ))
Copy after login
其他自己验证

extract()一般什么时候用

谢谢了,第一次知道$r['arr' . $v['project']][] = $v; 还可以让数组自己找的对应位置,其实这一句就到达我想要的结果了,我那种方法自己都觉得累

extract()之前看了下,大致就是把val直接给到$+key名的变量中

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template