我有来自DB的以下数组。
现在我希望得到[user] => shin
。
如何使用PHP获取它?
[29] => Array
中的数字可以是任意数字。
提前致谢。
Array
(
[29] => Array
(
[0] => stdClass Object
(
[day] => 29
[eventContent] => school ski tada
[eventTitle] => school ski
[id] => 24
[user] => shin
[user_id] => 1
)
[1] => stdClass Object
(
[day] => 29
[eventContent] => north again
[eventTitle] => ski hello
[id] => 26
[user] => shin
[user_id] => 1
)
)
[31] => Array
(
[0] => stdClass Object
(
[day] => 31
[eventContent] => test
[eventTitle] => test
[id] => 21
[user] => shin
[user_id] => 1
)
)
...
...
)
答案 0 :(得分:1)
我不确定数组索引代表什么,但我会留下它们以防万一它们对你有用。答案与Alix的答案基本相同,并且检查确保您实际拥有嵌套数组:
foreach ($array as $day => $events) {
if (is_array($events)) {
foreach ($events as $event) {
echo $day; // outputs 29 (same as $event->day)
echo $event->user; // outputs shin
echo $event->eventContent;
echo $event->eventTitle;
echo $event->id;
// etc, etc, etc.
}
}
}
注意外部foreach循环和内部循环之间的区别。外部的一个是检索数组键和值,而内部循环只返回数组值。
答案 1 :(得分:1)
答案 2 :(得分:0)
foreach ($yourArray as $value)
{
foreach ($value as $object)
{
echo $object->user;
}
}