Why does a two-dimensional array like this have only the last value input after foreach?

Source: Internet
Author: User
$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 '
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.