我有嵌套数组,我需要循环第二级深嵌套数组并进行一些匹配。
循环好友数组并比较reqs_status,其中user_id是登录用户($ id)。
Json数组如下所示:
[
{
"id":8,
"firstname":"Lauren",
"lastname":"Lambie",
"email":"lal30@org.uk",
"role":1,
"photo":"uploads\/5.jpg",
"bio":"Surfer, follower of Christ, band member and Swiss design-head.",
"friends":[
{
"id":8,
"user_id":9,
"friend_id":8,
"reqs_status":2,
"created_at":null,
"updated_at":null
}
]
},
{
"id":23,
"firstname":"Jar",
"lastname":"Morrison",
"email":"jm93@org.uk",
"role":1,
"photo":"uploads\/20.jpg",
"bio":"Operating at the crossroads of beauty and intellectual purity.",
"friends":[
{
"id":21,
"user_id":22,
"friend_id":23,
"reqs_status":2,
"created_at":null,
"updated_at":null
},
{
"id":28,
"user_id":21,
"friend_id":23,
"reqs_status":0,
"created_at":null,
"updated_at":null
}
]
}
]
在我的刀片视图中,我有:
@if ($user['friend'][0]['reqs_status'] == 1)
//do something
@elseif($user['friend'][0]['reqs_status'] == 2)
//do something elseif
@else
//do something else
这里我不知道[0]或[1],[2],....是否是正确的数字,因为它是动态的,并且数组的大小不固定。我需要一种循环这个嵌套数组的方法。
所以我试图将其分解为:
@foreach ($users['friends'] as $friend)
@if($friend['reqs_status'] == 1)
@elseif($friend['reqs_status'] == 2)
@else
@endforeach
我在这里得到错误:未定义索引:朋友。所以我在想如何让它变得动态[0]这一部分?或修复我的foreach循环代码和条件?