从数组中获取值时获取不正确的值
getting incorrect values while fetching values from an array
我需要从数组中获取值,为此我尝试参考此处给出的代码
我的数组是
Array
(
[success] => 1
[products] => Array
(
[0] => Array
(
[id] => 405
[name] => GOLD CHID
[categories] => Array
(
[0] => 39
[1] => 59
)
)
[1] => Array
(
[id] => 404
[name] => KASHMIRI
[categories] => Array
(
[0] => 39
[1] => 59
)
)
[2] => Array
(
[id] => 403
[name] => ENVELOPE BOX
[categories] => Array
(
[0] => 0
[1] => 39
[2] => 59
)
)
)
)
我用来获取数组的代码是
if (!empty($array))
{
foreach ($array['products'] as $product)
{
echo $product['id'];
echo "<br>";
echo $product['name'];
echo "<br>";
if(isset($product['categories']))
{
foreach($product['categories'] as $category)
echo $category['0'];
echo "<br>";
echo $category['1'];
echo "<br>";
}
}
}
我正在获取正确的 id 和名称值,但是我没有得到 $category['0']
和 $category['1']
的正确结果
我得到的结果是
405
GOLD CHID
35
9
404
KASHMIRI
35
9
403
ENVELOPE BOX
35
9
谁能告诉我哪里错了???
您的问题源于几个问题。
- 首先,你没有用大括号包裹你的第二个
foreach
循环
- 其次,您正在迭代类别,而您的代码并不期望这样做。
您的代码:
foreach($product['categories'] as $category)
echo $category['0'];
echo "<br>";
echo $category['1'];
echo "<br>";
有效:
foreach($product['categories'] as $category) {
echo $category['0'];
}
echo "<br>";
echo $category['1'];
echo "<br>";
循环遍历每个类别,打印每个类别中的第一个字符 (35
),然后打印最后一个类别中的第二个字符 (9
)。
将其更改为以下内容:
foreach($product['categories'] as $category) {
echo $category;
echo "<br>";
}
改变这个
foreach($product['categories'] as $category)
{
echo $category['0'];
echo "<br>";
echo $category['1'];
echo "<br>";
}
至此
foreach($product['categories'] as $category)
{
echo $category;
echo "<br>";
echo $category;
echo "<br>";
}
因为在您的代码中 $category[0]
将在 35
中输出第 0 个元素。 $category 将给出实际值
我需要从数组中获取值,为此我尝试参考此处给出的代码
我的数组是
Array
(
[success] => 1
[products] => Array
(
[0] => Array
(
[id] => 405
[name] => GOLD CHID
[categories] => Array
(
[0] => 39
[1] => 59
)
)
[1] => Array
(
[id] => 404
[name] => KASHMIRI
[categories] => Array
(
[0] => 39
[1] => 59
)
)
[2] => Array
(
[id] => 403
[name] => ENVELOPE BOX
[categories] => Array
(
[0] => 0
[1] => 39
[2] => 59
)
)
)
)
我用来获取数组的代码是
if (!empty($array))
{
foreach ($array['products'] as $product)
{
echo $product['id'];
echo "<br>";
echo $product['name'];
echo "<br>";
if(isset($product['categories']))
{
foreach($product['categories'] as $category)
echo $category['0'];
echo "<br>";
echo $category['1'];
echo "<br>";
}
}
}
我正在获取正确的 id 和名称值,但是我没有得到 $category['0']
和 $category['1']
我得到的结果是
405
GOLD CHID
35
9
404
KASHMIRI
35
9
403
ENVELOPE BOX
35
9
谁能告诉我哪里错了???
您的问题源于几个问题。
- 首先,你没有用大括号包裹你的第二个
foreach
循环 - 其次,您正在迭代类别,而您的代码并不期望这样做。
您的代码:
foreach($product['categories'] as $category)
echo $category['0'];
echo "<br>";
echo $category['1'];
echo "<br>";
有效:
foreach($product['categories'] as $category) {
echo $category['0'];
}
echo "<br>";
echo $category['1'];
echo "<br>";
循环遍历每个类别,打印每个类别中的第一个字符 (35
),然后打印最后一个类别中的第二个字符 (9
)。
将其更改为以下内容:
foreach($product['categories'] as $category) {
echo $category;
echo "<br>";
}
改变这个
foreach($product['categories'] as $category)
{
echo $category['0'];
echo "<br>";
echo $category['1'];
echo "<br>";
}
至此
foreach($product['categories'] as $category)
{
echo $category;
echo "<br>";
echo $category;
echo "<br>";
}
因为在您的代码中 $category[0]
将在 35
中输出第 0 个元素。 $category 将给出实际值