Home > Backend Development > PHP Tutorial > 请问一个数组整理方法

请问一个数组整理方法

WBOY
Release: 2016-06-13 12:05:19
Original
1028 people have browsed it

请教一个数组整理方法

<br />$abc = array(<br />    a1=>array(a=>1,b=>1,c=1,d=2),<br />    a2=>array(a=>1,b=>2,c=2,d=2),<br />    a3=>array(a=>3,b=>2,c=3,d=2)<br />)<br />
Copy after login

现在我需要将上面的二维数组合并为一维数组。
一维数组需要包含a=1 and b=1 这个数组的所有信息 和 a=3 这个数组的所有信息,我想请问一下,这种整理,使用一个foreach 可以整理出来么。
------解决方案--------------------
$abc = array(<br />  'a1' => array('a' => 1, 'b' => 1, 'c' => 1, 'd' => 2),<br />  'a2' => array('a' => 1, 'b' => 2, 'c' => 2, 'd' => 2),<br />  'a3' => array('a' => 3, 'b' => 2, 'c' => 3, 'd' => 2),<br />);<br /><br />$filter = array(<br />  array( 'a' => 1, 'b' => 1),<br />  array( 'a' => 3),<br />);<br /><br />$res = array();<br />foreach($abc as $item) {<br />  foreach($filter as $f) {<br />    if(array_intersect_assoc($f, $item) == $f)<br />      $res = array_merge($res, array_values($item));<br />  }<br />}<br />print_r($res);<br /><br />
Copy after login
Array<br />(<br />    [0] => 1<br />    [1] => 1<br />    [2] => 1<br />    [3] => 2<br />    [4] => 3<br />    [5] => 2<br />    [6] => 3<br />    [7] => 2<br />)<br /><br />
Copy after login

------解决方案--------------------
$abc = array(<br />    'a1'=>array('a'=>'1','b'=>'1','c'=>'1','d'=>'2'),<br />    'a2'=>array('a'=>'1','b'=>'2','c'=>'2','d'=>'2'),<br />    'a3'=>array('a'=>'3','b'=>'2','c'=>'3','d'=>'2')<br />);<br />$data1 = $data2 = array();<br />foreach ($abc as $key => $value) {<br />        $v1 = isset($value['a']) ? $value['a'] : 0;<br />        $v2 = isset($value['b']) ? $value['b'] : 0;<br />        if ($v1 == 1 && $v2 == 2) {<br />                $data1[] = $value;<br />        }<br />        if ($v1 == 3) {<br />                $data2[] = $value;<br />        }   ??<br />}           <br />print_r($data1);<br />            <br />print_r($data2);
Copy after login

------解决方案--------------------

<br /><?php<br />$abc = array(<br />    'a1'=>array('a'=>1,'b'=>1,'c'=>1,'d'=>2),<br />    'a2'=>array('a'=>1,'b'=>2,'c'=>2,'d'=>2),<br />    'a3'=>array('a'=>3,'b'=>2,'c'=>3,'d'=>2)<br />);<br /><br />$n = array();<br /><br />foreach($abc as $key=>$val){<br />    if($val['a']==1 && $val['b']==1 <br><font color='#FF8000'>------解决方案--------------------</font><br> $val['a']==3){<br />        $n = array_merge($n,array_values($val));<br />    }<br />}<br /><br />print_r($n);<br />?><br />
Copy after login

Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 2
[4] => 3
[5] => 2
[6] => 3
[7] => 2
)
------解决方案--------------------
<br />$abc = array(<br />  'a1' => array('a' => 1, 'b' => 1, 'c' => 1, 'd' => 2),<br />  'a2' => array('a' => 1, 'b' => 2, 'c' => 2, 'd' => 2),<br />  'a3' => array('a' => 3, 'b' => 2, 'c' => 3, 'd' => 2),<br />);<br />$new_arr1=array();<br />$new_arr2=array();<br />$item1=array('a'=>1,'b'=>1);<br />$item2=array('a'=>3);<br />foreach($abc as $k=>$v){<br />    $a=array_intersect_assoc($item1,$v);<br />    $b=array_intersect_assoc($item2,$v);<br />    if($a==$item1) $new_arr1=$v;<br />    if($b==$item2) $new_arr2=$v;<br />}<br />print_r(array_merge(array_values($new_arr1),array_values($new_arr2)));<br />
Copy after login


Related labels:
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