0

I am only interested in keys class and score and their values in my nested array. How can I only get these keys values to be printed out?

This is my $result array.

{  "images": [    {      "classifiers": [        {          "classifier_id": "default",          "name": "default",          "classes": [            {              "class": "banana",              "score": 0.562,              "type_hierarchy": "/fruit/banana"            },            {              "class": "fruit",              "score": 0.788            },            {              "class": "diet (food)",              "score": 0.528,              "type_hierarchy": "/food/diet (food)"            },            {              "class": "food",              "score": 0.528            },            {              "class": "honeydew",              "score": 0.5,              "type_hierarchy": "/fruit/melon/honeydew"            },            {              "class": "melon",              "score": 0.501            },            {              "class": "olive color",              "score": 0.973            },            {              "class": "lemon yellow color",              "score": 0.789            }          ]        }      ],      "image": "fruitbowl.jpg"    }  ],  "images_processed": 1,  "custom_classes": 0}

And this is my code logic.

foreach($result as $imgage => $classifier)
{
    foreach($classifier["classes"] as $clas)
    {
        foreach($clas as $key => $value)
        {
            echo $key . ": " . $value;
        }
    }
}
1
  • how about if ($key == "class" || $key == "score") echo $key . ": " . $value;? Commented Oct 3, 2018 at 15:58

2 Answers 2

2

This should work for you,

<?php
$string = '{"images":[{"classifiers":[{"classifier_id":"default","name":"default","classes":[{"class":"banana","score":0.562,"type_hierarchy":"/fruit/banana"},{"class":"fruit","score":0.788},{"class":"diet (food)","score":0.528,"type_hierarchy":"/food/diet (food)"},{"class":"food","score":0.528},{"class":"honeydew","score":0.5,"type_hierarchy":"/fruit/melon/honeydew"},{"class":"melon","score":0.501},{"class":"olive color","score":0.973},{"class":"lemon yellow color","score":0.789}]}],"image":"fruitbowl.jpg"}],"images_processed":1,"custom_classes":0}';
$array = json_decode($string,1);
foreach($array['images'] as $key => $images)
{
        foreach($images['classifiers'] as $key => $classes)
        {
            foreach($classes['classes'] as $cls_score){
                echo "class = ". $cls_score['class'].  " & score = ". $cls_score['score'].PHP_EOL;
            }
        }
}

WORKING DEMO: https://3v4l.org/vF2RL

Sign up to request clarification or add additional context in comments.

Comments

1

Can you try ?

$str = '{  "images": ....  }'; 

// get associative array from json
$result = json_decode($str, true)['images'];
foreach ($result as $imgage => $classifiers) {
    foreach ($classifiers["classifiers"] as $classifier) {
        foreach ($classifier["classes"] as $clas) {
            foreach ($clas as $key => $value) {
                // you can add condition here to target only desired keys
                echo $key . ": " . $value;
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.