为什么这样的二维数组foreach后只有最后一个值输入?
程序员文章站
2022-06-16 14:50:51
...
$res = array(
array(
'goods_id' => 14,
'goods_number' => 1
),
array(
'goods_id' => 16,
'goods_number' => 1
),
array(
'goods_id' => 18,
'goods_number' => 1
)
);
上面数组,我想遍历出goods_id 从14 到 18,也就是14,15,16,17,18 ,从而得到goods_id 对应的 goods_number,如果goods_id不存在数组里,goods_number就给予0给它。
foreach($res as $value)
{
if($value['goods_id'] == 14)
{
echo $value['goods_number'];
echo "
";
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 15)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 16)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 17)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 18)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
}
为什么我这样foreach 不行呢?
我要的结果是:
goods_id:14 时 goods_number:1
goods_id:15 时 goods_number:0
goods_id:16 时 goods_number:1
goods_id:17 时 goods_number:0
goods_id:18 时 goods_number:1
所以应该echo的结果是
1
0
1
0
1
为什么我的foreach 得到的不是这样的结果呢?
array(
'goods_id' => 14,
'goods_number' => 1
),
array(
'goods_id' => 16,
'goods_number' => 1
),
array(
'goods_id' => 18,
'goods_number' => 1
)
);
上面数组,我想遍历出goods_id 从14 到 18,也就是14,15,16,17,18 ,从而得到goods_id 对应的 goods_number,如果goods_id不存在数组里,goods_number就给予0给它。
foreach($res as $value)
{
if($value['goods_id'] == 14)
{
echo $value['goods_number'];
echo "
";
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 15)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 16)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 17)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
if($value['goods_id'] == 18)
{
echo $value['goods_number'];
}
else
{
echo 0;
echo "
";
}
}
为什么我这样foreach 不行呢?
我要的结果是:
goods_id:14 时 goods_number:1
goods_id:15 时 goods_number:0
goods_id:16 时 goods_number:1
goods_id:17 时 goods_number:0
goods_id:18 时 goods_number:1
所以应该echo的结果是
1
0
1
0
1
为什么我的foreach 得到的不是这样的结果呢?
回复讨论(解决方案)
$res = array( array( 'goods_id' => 14, 'goods_number' => 1 ), array( 'goods_id' => 16, 'goods_number' => 1 ), array( 'goods_id' => 18, 'goods_number' => 1 ) );$newArr = array();foreach ($res as $key => $value) { $newArr[$value['goods_id']] = $value['goods_number'];}//var_dump(array_values($res));exit;$range = range(14, 18);foreach ($range as $keys => $values) { if(array_key_exists($values, $newArr)){ echo 'goods_id:'.$values.' 时 goods_number:'.$newArr[$values]; echo '
'; }else{ echo 'goods_id:'.$values.' 时 goods_number:0'; echo '
'; }}
$res = array( array( 'goods_id' => 14, 'goods_number' => 1 ), array( 'goods_id' => 16, 'goods_number' => 1 ), array( 'goods_id' => 18, 'goods_number' => 1 ) );$newArr = array();foreach ($res as $key => $value) { $newArr[$value['goods_id']] = $value['goods_number'];}//var_dump(array_values($res));exit;$range = range(14, 18);foreach ($range as $keys => $values) { if(array_key_exists($values, $newArr)){ echo 'goods_id:'.$values.' 时 goods_number:'.$newArr[$values]; echo '学习了。
'; }else{ echo 'goods_id:'.$values.' 时 goods_number:0'; echo '
'; }}
$res = array( array( 'goods_id' => 14, 'goods_number' => 1 ), array( 'goods_id' => 16, 'goods_number' => 1 ), array( 'goods_id' => 18, 'goods_number' => 1 ));for($i=14; $i';}
上一篇: * 雪顿节晒佛 那曲赛马节
下一篇: 关于PHP公钥加密的有关问题