$res = Array (
Array
' goods_id ' = 14,
' Goods_number ' = 1
),
Array
' goods_id ' = 16,
' Goods_number ' = 1
),
Array
' goods_id ' = 18,
' Goods_number ' = 1
)
);
Above the array, I want to traverse out goods_id from 14 to 18, which is 14,15,16,17,18, thus get goods_id corresponding goods_number, if goods_id does not exist in the array, Goods_number gives 0 to it.
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 "
";
}
}
Why am I so foreach not?
The result I want is:
Goods_id:14 when Goods_number:1
Goods_id:15 when goods_number:0
Goods_id:16 when Goods_number:1
Goods_id:17 when goods_number:0
Goods_id:18 when Goods_number:1
So the echo should be the result
1
0
1
0
1
Why doesn't my foreach get this result?
Reply to discussion (solution)
$res = Array (' goods_id ' = +, ' goods_number ' = + 1), array (' goods_id ' = +, ' goods_number ' = 1), Array (' goods_id ' +, ' goods_number ' + 1), $NEWARR = Array (), foreach ($res as $key = = $value) {$NEWARR [$value [' Goods_i d ']] = $value [' Goods_number '];} Var_dump (Array_values ($res)), exit, $range = range (+), 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. ' When goods_number:0 '; Echo '
';}}
$res = Array (' goods_id ' = +, ' goods_number ' = + 1), array (' goods_id ' = +, ' goods_number ' = 1), Array (' goods_id ' +, ' goods_number ' + 1), $NEWARR = Array (), foreach ($res as $key = = $value) {$NEWARR [$value [' Goods_i d ']] = $value [' Goods_number '];} Var_dump (Array_values ($res)), exit, $range = range (+), 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. ' When goods_number:0 '; Echo '
';}}
Study.
$res = Array (' goods_id ' = + , ' goods_number ' + 1), array (' goods_id ' = +, ' goods_number ' =&G T 1), array (' goods_id ', ' goods_number ' = 1)), for ($i =14; $i <=18; $i + +) { if ($i = = $res [0][' goods_i d ']) { echo $res [0][' Goods_number ']; Array_shift ($res); } else echo 0; Echo '