Home >Backend Development >PHP Tutorial >数组用下标来重组数组,为什么重组出来的数组下标一直是第一个?
<?php$id = array(8,31,32);$resarr = array();foreach($id as $num){ $product = array( array( 'pid'=>'10', 'name'=>'产品1' ), array( 'pid'=>'11', 'name'=>'产品2' ) ); foreach($product as $key => $value) { $result_value = ""; foreach($value as $k => $v) { $result_value .= $result_value ? "|" : ""; $result_value .= $v['pid']; } $product[$key]['value'] = $result_value; } $resarr[$num] = $product; var_dump($resarr); echo "<hr />";}
在循环外面打印
$id = array(8,31,32);$resarr = array();foreach($id as $num){ $product = array( array( 'pid'=>'10', 'name'=>'产品1' ), array( 'pid'=>'11', 'name'=>'产品2' ) ); foreach($product as $key => $value) { $result_value = ""; foreach($value as $k => $v) { $result_value .= $result_value ? "|" : ""; $result_value .= $v['pid']; } $product[$key]['value'] = $result_value; } $resarr[$num] = $product; }var_dump($resarr);echo "<hr />";
谢谢楼上2位,我太大头虾了。