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

WBOY
Release: 2016-06-23 14:02:08
Original
1105 people have browsed it

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


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 />";
Copy after login

谢谢楼上2位,我太大头虾了。

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