2016-05-16 76 views
0

我有一個JSON字符串,如下面給出的JSON字符串存儲在$json。我想訪問第一個category_id而不使用循環。我怎樣才能訪問它?訪問一個沒有循環的嵌套JSON密鑰

$json = '{"outfits": [{"1": [{"category_id": "women_jeans", "product_id": 464540467}, {"category_id": "women_tops", "product_id": 487351815}, {"category_id": "women_coats", "product_id": 493322686}, {"category_id": "women_bags", "product_id": 483902882}, {"category_id": "women_shoes", "product_id": 492772225}]}]}'; 
$outfits = json_decode($json); 
+1

你有多個'category_id'可利用的。哪一個你到底要訪問? – Nikola

+0

@Nikola:第一個有product_id:464540467 –

+0

$ outfits = json_decode($ json,true); var_dump($ outfits ['outfits'] [0] [1] [0] [「category_id」]); –

回答

5

嘗試以下代碼。

$json = '{"outfits": [{"1": [{"category_id": "women_jeans", "product_id": 464540467}, {"category_id": "women_tops", "product_id": 487351815}, {"category_id": "women_coats", "product_id": 493322686}, {"category_id": "women_bags", "product_id": 483902882}, {"category_id": "women_shoes", "product_id": 492772225}]}]}'; 
$outfits = json_decode($json,true); 
echo "Category 1: ".$outfits["outfits"][0][1][0]['category_id']; 
echo "Category 2: ".$outfits["outfits"][0][1][1]['category_id']; 

輸出

women_jeans 
women_tops 

您也可以使用以下功能查找特定的值。

$array = $outfits["outfits"][0][1]; 
$key = "product_id"; 
$val = "464540467"; 

function whatever($array, $key, $val) { 
    foreach ($array as $item) 
     if (isset($item[$key]) && $item[$key] == $val) 
      echo $item['category_id']; 
    return false; 
} 

whatever($array,$key,$val); 

輸出

women_jeans 

如果你想獲得無CATEGORY_ID環和true然後用下面。

$array = $outfits->outfits[0]->{1}; 
$category1 = $array[0]->category_id; 
+0

使用上面的函數獲取第一個產品ID:464540467. @Rakesh Sojitra – RJParikh

+0

是的,我只是忘了寫json_decode($ json,true);真正。這是問題。 –

+0

你可以做到這一點,沒有'true'。 [檢查我的答案](http://stackoverflow.com/questions/37252027/how-can-i-access-category-id/37252438#37252438) –

0

例如,

<?php 
$json = '{"outfits": [{"1": [{"category_id": "women_jeans", "product_id": 464540467}, {"category_id": "women_tops", "product_id": 487351815}, {"category_id": "women_coats", "product_id": 493322686}, {"category_id": "women_bags", "product_id": 483902882}, {"category_id": "women_shoes", "product_id": 492772225}]}]}'; 
$outfits = json_decode($json, true); 


$arr = $outfits["outfits"][0][1]; 

值的直接訪問:

echo $arr[0]['category_id'].", ".$arr[1]['category_id']; 

function walk($v, $k) { 
    echo "$k : ".$v['category_id']."<br/>"; 
} 

您可以使用array_walk,如果你不想讓自己的循環:

array_walk($arr, "walk"); 
?> 
2
$json = '{"outfits": [{"1": [{"category_id": "women_jeans", "product_id": 464540467}, {"category_id": "women_tops", "product_id": 487351815}, {"category_id": "women_coats", "product_id": 493322686}, {"category_id": "women_bags", "product_id": 483902882}, {"category_id": "women_shoes", "product_id": 492772225}]}]}'; 
$outfits = json_decode($json,true); 
+0

是的,我只是發現我的錯誤在json_decode($ json,true);不管怎樣,謝謝。 –

0
$json= '{"outfits": [{"1": [{"category_id": "women_jeans", "product_id": 464540467}, {"category_id": "women_tops", "product_id": 487351815}, {"category_id": "women_coats", "product_id": 493322686}, {"category_id": "women_bags", "product_id": 483902882}, {"category_id": "women_shoes", "product_id": 492772225}]}]}'; 

//convert json to associative array. 
$json_array = json_decode($json,true); 

// Return the values from a single column in the input array 
$newArray= array_column($json_array["outfits"][0][1],"category_id"); 

輸出

Array 
(
    [0] => women_jeans 
    [1] => women_tops 
    [2] => women_coats 
    [3] => women_bags 
    [4] => women_shoes 
) 

注意:我想沒有循環來訪問它array_column只能從5.5+

0

OP的要求這不是 正確的原因你有多個category_id,所以問題是 哪一個你需要。所以你需要循環。

通過使用json_decode您將有一個對象數組,因此您需要執行這些操作。讓$json是你的字符串。通過在foreach循環中使用$outfits->outfits[0]->{1}只需獲取結果數組的最後一個維度,便於訪問category_id

$outfits = json_decode($json); 

foreach($outfits->outfits[0]->{1} as $val){ 
    echo $val->category_id."<br/>"; 
} 

結果:

women_jeans 
women_tops 
women_coats 
women_bags 
women_shoes