Home >Backend Development >PHP Tutorial >数组用下标来重组数组,为什么重组出来的数组下标一直是第一个?

数组用下标来重组数组,为什么重组出来的数组下标一直是第一个?

WBOY
WBOYOriginal
2016-06-23 14:02:081117browse

<?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 />";}


var_dump() 出来的,下标一直都是[8]这个。。。哪里写错了?


回复讨论(解决方案)

在循环外面打印

$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位,我太大头虾了。

Statement:
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