从多维数组中获取值
Fetch values from multi dimensional array
我需要从通过
得到的数组中获取数据
print_r($result);
我得到的数组是
Array
(
[0] => Array
(
[id] =>
[Location] => Array
(
[img] => 177223
[name] =>
)
[Max] =>
[Total] =>
[Description] => Array
(
[Pre] =>
[Updated] =>
[Program] => Array
(
[Schedule] =>
)
)
[Staff] => Array
(
[FirstName] =>
)
)
)
我使用了这个代码
if (!empty($result))
{
foreach ($result as $res)
{
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location']))
{
foreach($res['Location'] as $loc)
{
$img= $loc['img'];
echo $img;
echo "<br>";
}
}
}
}
我得到第一个数组的正确值(即 Max 等),但位置、描述和员工的值不正确,任何人都可以更正我的代码
位置不是数组的数组。
它只是一个关联数组。
if (!empty($result)) {
foreach ($result as $res) {
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location'])) {
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
}
}
您不需要通过位置进行 foreach,只需直接访问它的元素即可:
if(isset($res['Location']))
{
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
或者类似的东西。
我需要从通过
得到的数组中获取数据print_r($result);
我得到的数组是
Array
(
[0] => Array
(
[id] =>
[Location] => Array
(
[img] => 177223
[name] =>
)
[Max] =>
[Total] =>
[Description] => Array
(
[Pre] =>
[Updated] =>
[Program] => Array
(
[Schedule] =>
)
)
[Staff] => Array
(
[FirstName] =>
)
)
)
我使用了这个代码
if (!empty($result))
{
foreach ($result as $res)
{
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location']))
{
foreach($res['Location'] as $loc)
{
$img= $loc['img'];
echo $img;
echo "<br>";
}
}
}
}
我得到第一个数组的正确值(即 Max 等),但位置、描述和员工的值不正确,任何人都可以更正我的代码
位置不是数组的数组。 它只是一个关联数组。
if (!empty($result)) {
foreach ($result as $res) {
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location'])) {
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
}
}
您不需要通过位置进行 foreach,只需直接访问它的元素即可:
if(isset($res['Location']))
{
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
或者类似的东西。