请教PHP算法,二维数组取出一维数组中id相同为几个新的二维数组。
小弟刚学PHP不久,遇到了一个问题,请教大侠们,谢谢!
二维数组如下;
$arr = array(
array('id'=>100,'value'=>100),
array('id'=>100,'value'=>130,
array('id'=>100,'value'=>140),
array('id'=>100,'value'=>150),
array('id'=>101,'value'=>1600),
array('id'=>101,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>105,'value'=>100)
);
如何拆分成下面5个二维数组:
arr1(
array('id'=>100,'value'=>100),
array('id'=>100,'value'=>130,
array('id'=>100,'value'=>140),
array('id'=>100,'value'=>150)
);
arr2(
array('id'=>101,'value'=>1600),
array('id'=>101,'value'=>100)
);
arr3(
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100)
);
arr4(
array('id'=>103,'value'=>100),
array('id'=>103,'value'=>100)
);
arr5(
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>105,'value'=>100)
);
arr5(
array('id'=>105,'value'=>100)
); PHP 二维数组 算法
[解决办法]
$arr = array(
array('id'=>100,'value'=>100),
array('id'=>100,'value'=>130),
array('id'=>100,'value'=>140),
array('id'=>100,'value'=>150),
array('id'=>101,'value'=>1600),
array('id'=>101,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>105,'value'=>100)
);
foreach($arr as $key=>$val){
$newarr[$val['id']][]=array('id'=>$val['id'],'value'=>$val['value']);
}
$arr1=$newarr['100'];
$arr2=$newarr['101'];
$arr3=$newarr['102'];
$arr4=$newarr['103'];
$arr5=$newarr['104'];
$arr6=$newarr['105'];
$arr = array(
array('id'=>100,'value'=>100),
array('id'=>100,'value'=>130),
array('id'=>100,'value'=>140),
array('id'=>100,'value'=>150),
array('id'=>101,'value'=>1600),
array('id'=>101,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>105,'value'=>100)
);
foreach($arr as $v) $res[$v['id']][] = $v;
//print_r($res);//你可以直接使用这个数组
//也可以导入变量表后访问
extract(array_values($res), EXTR_PREFIX_ALL, 'arr');
//如
print_r($arr_3);
//代码同上
//此段代码直接运行下即可
$arr = array (
array ('id' => 100, 'value' => 100 ),
array ('id' => 100, 'value' => 130 ),
array ('id' => 100, 'value' => 140 ),
array ('id' => 100, 'value' => 150 ),
array ('id' => 101, 'value' => 1600 ),
array ('id' => 101, 'value' => 100 ),
array ('id' => 102, 'value' => 100 ),
array ('id' => 102, 'value' => 100 ),
array ('id' => 102, 'value' => 100 ),
array ('id' => 103, 'value' => 100 ),
array ('id' => 103, 'value' => 100 ),
array ('id' => 104, 'value' => 100 ),
array ('id' => 104, 'value' => 100 ),
array ('id' => 104, 'value' => 100 ),
array ('id' => 105, 'value' => 100 ) );
foreach($arr as $v) $res[$v['id']][] = $v; //my_debug($res);//你可以直接使用这个数组 //也可以导入变量表后访问
extract(array_values($res), EXTR_PREFIX_ALL, 'arr');
my_debug($arr_0);
my_debug($arr_1);
my_debug($arr_2);
my_debug($arr_3);
function my_debug($v = NULL, $var_name = NULL, $method = 'dump', $backtrace = false) {
$display_none = '';
if (defined ( 'FRONTPAGE' )) {
$display_none = "display:none;";
}
("<div style="background-color:#AAAAAA;padding:5px;border:solid 1px #FF0000;margin:5px;$display_none">") ;
$call_stack = debug_backtrace ();
printf ( 'file:%s ,%s(),<font color="red">line:%s</font><br>', @$call_stack [0] ['file'],
@$call_stack [1] ['function'], @$call_stack [0] ['line'] );
if (! $var_name) {
//取$v在源代码中的变量名字.
$v_line = file ( $call_stack [0] ['file'] );
$f_line = $v_line [$call_stack [0] ['line'] - 1];
$match = array ();
preg_match ( '#my_debug.*[(].*#', $f_line, $match );
if (count ( $match )) {
$var_name = $match [0];
}
}
if ($var_name) {
printf ( 'debug: <font color="blue">%s</font>', $var_name );
}
//xdebug_var_dump ( $v );
print ('<pre style="border:solid 1px #0000CC;padding:5px;">') ;
if ($method == 'dump') {
var_dump ( $v );
} else {
print_r ( $v );
}
print ("</pre>") ;
if ($backtrace) {
foreach ( debug_backtrace () as $v ) {
echo ($v ['file'] . ":" . $v ['line'] . "," . $v ['class'] . "," . $v ['function'] . "()\n<br>\n");
}
}
print ('</div>') ;
print ("\n") ;
}