我有一个数组,我想从数组中提取一些数据。这通常对我来说没有问题,但这次对我来说有点不同
我有这个阵列:
$arrParcelLabelNumber = array
(
"parcelLabelNumber" => array
(
0 => "10",
1 => "20"
),
"weight" => array
(
0 => "1111",
1 => "2222"
)
);
我希望得到以下结果:10=1111,20=2222
这意味着我想要parcelLabelNumber的值,其中键与权重中的键相同,然后我也想要权重的值,它们都相同。
我尝试了几种方法,但都不起作用。
新代码:
$arrParcelLabelNumber = array
(
"parcelLabelNumber" => array
(
0 => "09988014801055",
1 => "09988014801056"
),
"weight" => array
(
0 => "1111",
1 => "2222"
)
);
foreach($arrParcelLabelNumber as $val){
foreach($val['parcelLabelNumber'] as $key=>$v){
echo $val['weight'][$key] . "=" . $v;
}
}
您也可以使用一个简单的for:
$c = count($arrParcelLabelNumber["parcelLabelNumber"]);
for ($i = 0; $i< $c; $i++)
{
if(isset($arrParcelLabelNumber["parcelLabelNumber"][$i]) && isset($arrParcelLabelNumber["weight"][$i])){
echo $arrParcelLabelNumber["parcelLabelNumber"][$i] . " = " . $arrParcelLabelNumber["weight"][$i];
}
}
foreach($arrParcelLabelNumber as $val){
foreach($val['parcelLabelNumber'] as $key=>$v){
echo $val['weight'][$key] . "=" . $v;
}
}
或者,如果你想将它们存储为键值对以供检索。。。
$newarray=array();
foreach($arrParcelLabelNumber as $val){
foreach($val['parcelLabelNumber'] as $key=>$v){
$newarray[$val['weight'][$key]] = $v;
}
}
var_dump($newarray);
或者作为数组中的一对。。。
$newarray=array();
foreach($arrParcelLabelNumber as $val){
foreach($val['parcelLabelNumber'] as $key=>$v){
$newarray[]=array('parcelLabelNumber'=>$v,'weight'=> $val['weight'][$key]);
}
}