我正在使用API进行项目,并希望将API响应数据分为2个数组(对与错)。我该怎么办?
API端点返回此:
{
"data": [{
"itemId": "d930dba-8e19b63-a6d5d94-6978fe3",
"lastUpdate": 1556668801,
"store": {
"isFeatured": true,
"isRefundable": true,
"cost": "1500"
},
"item": {
"name": "Star-Lord Outfit",
"description": "Do something good, something bad, a bit of both!",
"type": "outfit",
"rarity": "marvel",
"images": {
"icon": "https:\/\/fortnite-public-files.theapinetwork.com\/outfit\/a6bac035952059b94ba5e665f6f5a0f0.png",
"featured": "https:\/\/fortnite-public-files.theapinetwork.com\/featured\/d930dba-8e19b63-a6d5d94-6978fe3.png",
"background": "https:\/\/fortnite-public-files.theapinetwork.com\/image\/d930dba-8e19b63-a6d5d94-6978fe3.png",
"information": "https:\/\/fortnite-public-files.theapinetwork.com\/image\/d930dba-8e19b63-a6d5d94-6978fe3\/item.png"
},
"obtained_type": "vbucks",
"ratings": {
"avgStars": 4.06,
"totalPoints": 1691,
"numberVotes": 416
}
}
},
我尝试了很多事情,这是一个:
<?php
for ($i = 0; $i < count($image_data['data'][$i]['store']['isFeatured']['true']); $i ++) { }
?>
答案 0 :(得分:0)
您可以使用PHP array_filter()函数。这是一个示例代码,它将满足您的需求:
function getFeaturedImages($item){
return $item['store']['isFeatured'];
}
function getNonFeaturedImages($item){
return !$item['store']['isFeatured'];
}
$featuredImageDataArray = array_filter($image_data['data'], 'getFeaturedImages');
$nonFeaturedImageDataArray = array_filter($image_data['data'], 'getNonFeaturedImages');
$featuredImageArray = ['data'=>[]];
$nonFeaturedImageArray = ['data'=>[]];
foreach($featuredImageDataArray as $value)
$featuredImageArray['data'][] = $value;
foreach($nonFeaturedImageDataArray as $value)
$nonFeaturedImageArray['data'][] = $value;
最后,您$featuredImageArray
将保存isFeatured = TRUE
的数据,而$nonFeaturedImageArray
将保存isFeatured = FALSE
的数据