【问题标题】:PHP loop through nested JSON to find valuePHP 循环遍历嵌套的 JSON 以查找值
【发布时间】:2017-11-16 17:46:13
【问题描述】:

我正在尝试遍历嵌套的 JSON 数据以查找具有特定字符串的名称并提取与之关联的字符串值。在这种情况下,名称字符串是“myKey”,值字符串是“12345678”。

在查看了几个类似的问题和文档后,我尝试了不同的方法,例如使用对象或关联数组,但最终还是无法获得我想要的信息或收到错误。

错误类型:

Notice: Array to string conversion

Warning: Invalid argument supplied for foreach()

Trying to get property of non-object

这是使用 $myObj = json_decode($result); 解码的 JSON 的 sn-p

object(stdClass)#4 (3) {
  ["info"]=>
  object(stdClass)#5 (10) {
    .
    .
    .
  }
  ["stuff"]=>
  array(1) {
    .
    .
    .
  }
  ["result"]=>
  array(3) {
    [0]=>
    object(stdClass)#7 (3) {
      ["name"]=>
      ["value"]=>
      ["description"]=>
    }
    [1]=>
    object(stdClass)#8 (2) {
      ["name"]=>
      string(4) "Units"
      ["value"]=>
      array(2) {
        [0]=>
        array(6) {
          [0]=>
          object(stdClass)#9 (3) {
            .
            .
            .
          }
          .
          .
          .
          [5]=>
          object(stdClass)#14 (2) {
            ["name"]=>
            string(10) "Components"
            ["value"]=>
            array(1) {
              [0]=>
              array(14) {
                [0]=>
                object(stdClass)#15 (3) {
                .
                .
                .
                }
                [1]=>
                object(stdClass)#16 (3) {
                  ["name"]=>
                  string(5) "myKey"
                  ["value"]=>
                  string(8) "12345678"
                  ["description"]=>
                }
                .
                .
                .

这是我尝试过的 PHP 代码的 sn-p:

$myObj = json_decode($result);
// or I have tried
// $myObj = json_decode($result, true);


// here are different snippets of code I tried
foreach($myObj->result as $test) {
    echo '<pre>';
    print_r($test->name);
    echo "<br>";
    if ($test->name == "Units") {
        $resultName = $test->name;
        echo $resultName . "<br>";
    }
    echo '</pre>';
}

/*
foreach($myObj->result as $test) {
    echo $test . "<br>";
    foreach($test->name as $test1) {
        echo $test1 . "<br>";
        foreach($test1->value as $test2) {
            echo $test2 . "<br>";
        }
    }
}
*/

/*
foreach($myObj->result as $test) {
    if (($test->name) == "Units") {
        // grab the value that corresponds to the name
        $units = $test->name;
        if (($units->name) == "Components") {
            $components = $units->name;
            print_r($components);
        }
    }
}
*/

我可以通过以下方式直接访问我需要的内容:

print_r($myObj->result[1]->value[0][5]->value[0][1]->name);
print_r($myObj->result[1]->value[0][5]->value[0][1]->value);

但值的位置可能会有所不同,所以我需要通过循环查找对象的名称

谁能提供使用对象(甚至可能是关联数组)的更好方法?

更新后包含原始 JSON 的片段(解码前)

string(21420) "{
  "info": {
  .
  .
  .
  },
  "stuff": [{
    "name":
    "type":
    .
    .
    .
  }],
  "result": [
    {
      "name":
      "value":
      "description":
    },
    {
      "name": "Units",
      "value": [
        [
          {
            "name":
            "value":
            "description":
          },
          .
          .
          .
          {
            "name": "Components",
            "value": [
             [
               {
                 "name":
                 "value":
                 "description":
               },
               {
                 "name": "myKey",
                 "value": "12345678",
                 "description":
               },
               .
               .
               .
             ] (end inner Components value)
           ] (end outer Components value)
         ] (end inner Units value)
       ] (end outer Units value)
     } (end results Units)
   ] (end result)
 } (end opening)

【问题讨论】:

  • 您是否考虑过将此结果转换为新的(关联)数组,以便您可以管理键/位置并轻松循环数组?
  • 嗯我只看到 $result 是一个数组(3),而不是一个对象。
  • @Adder 结果是一个对象数组;)
  • 你能发布一个你必须转换的原始json的样本吗?
  • @Eineki 我已更新帖子以在 json_decode 函数之前显示原始 JSON 的 sn-p

标签: php arrays json object


【解决方案1】:

感觉就像你需要一些递归函数(一个在找到结果之前调用自身的函数)来查找嵌套数组中的值。

看看Recursive array_search

当然,您必须更改该问题中的功能,但是我曾经遇到过类似的问题,并且非常有用。

【讨论】:

    【解决方案2】:

    下面是编码和解码的代码。希望对你有帮助

     //Encoding 
        //Array Values
            $a1=array("name"=>"abc", "value"=>"def", "description"=>"ghi");
            $a2=array("name"=>"jkl", "value"=>"mno", "description"=>"pqr");
            $a3=array("name"=>"stu", "value"=>"wxy", "description"=>"zab");
        //Array of Value Arrays
            $info=array($a1, $a2, $a3);
            $result=array($a1, $a2, $a3);
            $stuff=array($a1, $a2, $a3);
        //The Main Enclosed Array
            $main=json_encode(array("info"=>$info, "result"=>$result, "stuff"=>$stuff));
    
        //Decoding  
        //Fetching the name from result's $a1 array
            $main_array=json_decode($main);
            echo $main_array->result[1]->name; //Displays jkl
    

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多