Heim > Artikel > Backend-Entwicklung > 请问一个数组整理方法
请教一个数组整理方法
<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 />
$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 />
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 />
$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);
<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 />
<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 />