在每次迭代中访问嵌套数组中的特定键/值

时间:2018-10-03 15:50:40

标签: php

我只对键classscore及其嵌套数组中的值感兴趣。我如何才能只打印出这些键值?

这是我的$result数组。

{  "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}

这是我的代码逻辑。

foreach($result as $imgage => $classifier)
{
    foreach($classifier["classes"] as $clas)
    {
        foreach($clas as $key => $value)
        {
            echo $key . ": " . $value;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

这应该对您有用,

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

工作演示: https://3v4l.org/vF2RL

答案 1 :(得分:1)

可以尝试吗?

   @override
    Widget build(BuildContext context) {
        return MaterialApp(
          home: FutureBuilder(
            initialData: "",
            future: _jHelp.getJson(_localKey),
            builder: (context, snapshot) {
              return Scaffold(
                appBar: AppBar(
                    title: Text(snapshot.data),
                    ),
                body: Center(
                  child: Column(
                    children: [
                      TextField(
                        controller: _textCTRL,
                        decoration: InputDecoration(labelText: 'write here'),
                      ),

                      RaisedButton(onPressed:_pressButton)
                    ],
                  ),
                ),
              );
            },
          ),
        );
      }