Rakesh Sojitra
Rakesh Sojitra

Reputation: 3658

Accessing a nested JSON key without looping

I have a JSON string like the one given below which is stored in $json. I want to access the first category_id without using a loop. How can I access it?

$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);

Upvotes: 0

Views: 1684

Answers (5)

RJParikh
RJParikh

Reputation: 4166

Try below code.

$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'];

Output

women_jeans
women_tops

Also you can use below function to find particular value.

$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);

Output

women_jeans

If you want to get category_id without loop and true then use below.

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

Upvotes: 6

Murad Hasan
Murad Hasan

Reputation: 9583

OP's Requirement I want to access it without loop. This is not correct cause you have multiple category_id, So the question is which one you need. So you need loop.

By using json_decode you will have an object array, so you need to do these. Let $json is your string. By using $outfits->outfits[0]->{1} in the foreach loop just get the last dimension of the resultant array, so its easy to access the category_id.

$outfits = json_decode($json);

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

Result:

women_jeans
women_tops
women_coats
women_bags
women_shoes

Upvotes: 0

user6123296
user6123296

Reputation: 1

$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");

Output

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

Note: array_column only available from 5.5+

Upvotes: 0

Jenson M John
Jenson M John

Reputation: 5689

Eg.

<?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];

Direct access of values :

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

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

You can use array_walk if you don't want your own Loop :

array_walk($arr, "walk");
?>

Upvotes: 0

Bindesh Pandya
Bindesh Pandya

Reputation: 193

 $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);

Upvotes: 2

Related Questions