2 回答
TA贡献1852条经验 获得超7个赞
您实际上不需要在这里嵌套循环,因为第一级不是数组,而是对象。
$data = "{"categories": [{"parent_id": 15397, "has_children": true, "category_id": 15402, "category_name": "AC"}, {"parent_id": 0, "has_children": true, "category_id": 15397, "category_name": "A"}], "request_id": "3beac0674e937d2471e4b66b5f998976"}";
// Change to Array
$obj = json_decode($data, true);
// Get the data
foreach($obj['categories'] as $category) {
print_r($category['category_id']);
}
TA贡献1909条经验 获得超7个赞
<?php
// Use this function to get the desired result, and then you can pass the result to another function
$data = '{"categories": [{"parent_id": 15397, "has_children": true, "category_id": 15402, "category_name": "AC"}, {"parent_id": 0, "has_children": true, "category_id": 15397, "category_name": "A"}], "request_id": "3beac0674e937d2471e4b66b5f998976"}';
function getCategoreIds($data=[]){
$cids=[];
$datas = json_decode($data,true);
if($datas['categories']){
foreach($datas['categories'] as $k=>$tmp){
if(isset($tmp['category_id'])){
$cids[] = $tmp['category_id'];
}
}
}
return $cids;
}
print_r(getCategoreIds($data));
- 2 回答
- 0 关注
- 225 浏览
添加回答
举报