我有一个看起来像这样的数组,除了超过40个
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => name
[cost] => 20
[firstname] =>
(
[0] => first 1
[1] => first 2
)
[lastname] =>
(
[0] => last 1
[1] => last 2
)
[username] =>
(
[0] => username 1
[1] => username 2
)
)
[0] => stdClass Object
(
[id] => 1
[name] => name
[cost] => 20
[firstname] =>
(
[0] => first 1
[1] => first 2
)
[lastname] =>
(
[0] => last 1
[1] => last 2
)
[username] =>
(
[0] => username 1
[1] => username 2
)
)
(
我正在做这样的事情来获得非嵌套数组:
foreach($data as $row){
echo $row->id
}
但是我该怎么做才能获得嵌套数组?我希望能够将它们显示为:
firstname lastname
用户名
答案 0 :(得分:1)
foreach($data as $row){
echo $row->firstname[0] . ' ' . $row->lastname[0] . '<br/>' . $row->username[0];
}
编辑:
如果这些人进行了产品评论,那么您应该更好地组织阵列:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => name
[cost] => 20
[reviews] => Array(
[0]=>
(
[firstname] => "Bob"
[lastname] => "Jacob"
[username] => "bobjacob543"
)
[1] = >
(...
)
)
[0] => stdClass Object
(
然后你可以做
foreach($data as $row){
foreach($row->reviews as $review){
echo $review->firstname . ' ' . $review->lastname . '<br/>' . $review->username;
}
...
}