来自Json Data的PHP中的多维数组挑战[重复]

问题描述 投票:0回答:1

我确实一直在努力正确地输出我从rest API响应接收到的json数据的内容。我已经搜索了很多并尝试了不同的方法,但是我注意到每个人的始终都是唯一的...下面是我的代码。特别是考虑到的是“ headers_field”,它是一个包含许多值和键的数组...我想要也可以通过键输出这些值...虽然(“ data”)带有两个值...下面也是我尝试过的方法,但是我只是从循环中返回(“ headers_field “:Array),(” box:Array“)..我使用了Curl。请看下面。谢谢。

{
"data": [
    {
        "approved": null,
        "approved_user_id": null,
        "boxes": [],
        "created": "2020-05-19T21",
        "created_at": "2020-05-19T21",
        "edited": null,
        "edited_in_affiliate": null,
        "edited_in_hdr": null,
        "edited_in_web_app": null,
        "erp_project_id": null,
        "error_description": "",
        "header_fields": [
            {
                "boxes": [],
                "code": "creditor_number",
                "data_type": "",
                "error": "",
                "feature_id": nvnnn,
                "value": "hhhhh"
            },
            {
                "boxes": [],
                "code": "payment_account_number",
                "data_type": "",
                "error": "",
                "value": ""
            },
            {
                "boxes": [],
                "code": "order_number",
                "data_type": "",
                "error": "",
                "value": ""
            },
            {
                "boxes": [],
                "code": "total_amount_incl_vat",
                "data_type": "",
                "error": "",
                "feature_id": mmm
                "value": "hhh"
            },
   {
        "approved": null,
        "approved_user_id": null,
        "boxes": [],
        "created": "2020-05-19",
        "created_at": "2020-05-19",
        "edited": null,
        "edited_in_affiliate": null,
        "edited_in_hdr": null,
        "edited_in_web_app": null,
        "erp_project_id": null,
        "error_description": "",
        "header_fields": [
            {
                "boxes": [],
                "code": "reference",
                "data_type": "",
                "error": "",
                "value": ""
            },
            {
                "boxes": [],
                "code": "payment_swift_bic",
                "data_type": "",
                "error": "",
                "value": ""
            },

            {
                "boxes": [],
                "code": "recipient_vat_reg_no",
                "data_type": "",
                "error": "",
                "value": ""
            },

            {
                "boxes": [],
                "code": "joint_payment_id",
                "data_type": "",
                "error": "",
                "value": ""
            },

            {
                "boxes": [],
                "code": "catalog_debitor_id",
                "data_type": "",
                "error": "",
                "value": ""
            },

            {
                "boxes": [],
                "code": "case_number",
                "data_type": "",
                "error": "",
                "value": ""
            },


            {
                "boxes": [],
                "code": "payment_account_number",
                "data_type": "",
                "error": "",
                "value": ""
            },
            {
                "boxes": [],
                "code": "payment_iban",
                "data_type": "",
                "error": "",
                "value": ""
            }


      ],
        "id": 00000,
        "image": "https://skkkkkllllllllll
        "line_items": [],
        "note": "",
        "organization_id": 11111,
        "organization_voucher_id": 7777,
        "page_count": 1,
        "status": "successful",
        "type": "voucher",
        "upload_type": "affiliate",
        "uploaded_by_user_id": 0000001
        "user_payment_method": "",
        "user_voucher_type": ""
   }
   ],
    "meta": {
    "count": 2
   }
   }

我的圈操作

  .....
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$results= curl_exec($ch);

   $g= json_decode($results, true);

   $i=0;
  $g_length =count($g["data"]);//to count through the child array

  for ($i=0; $i < ($g_length);$i++)
   {

      $k= $g["data"][$i]; //asssigning the array position to a variable

      foreach($k as $mydata=>$mvalue) {
    $hey = trim($mvalue); //Removes white spaces
    if ($mvalue !=="")
    echo $mydata. " : " . $mvalue . "<br>"; 

    else

    echo "This is empty. <br/>";


    ///////////////////////////////////////////
    foreach($mvalue as $lkey=>$lvalue)
    {
    $u=is_array($mvalue);
    if(($u) and !empty($u))
    echo $lkey. ":" . $lvalue. "<br>";
    else

    echo "what? <br/>";
    ///////////
    foreach($lvalue as $okey=>$ovalue)
    {

    echo $okey. ":". $value. "</n>";
    /////
 foreach($ovalue as $key=>$value)
 {
      $v=is_array($value);
      if(($v) and !empty($v))
      echo $key. ":" . $value. "<br>";
      else
    echo "hello";
      //print_r($key);

      /////
  }
    }
    //////////
  }

  //////////////////////////////////////////
 }
php json rest curl multidimensional-array
1个回答
0
投票

只要您知道从API返回的数组有多深或有多少维,并且知道密钥结构将是相同的,就可以使用如下代码:

[注意:我拿走了您的JSON,并快速又肮脏地吐出了所有键和值-包括嵌套的标头字段。本质上是基于值是否为ARRAY的。如果您知道期望的结构,则可以通过创建一个数组映射和一个函数来处理所有属于子数组的val来使它更加优雅。

无论如何-希望以下内容对您有所帮助:

foreach($theDecodedData['data']["header_fields"] as $row){

    foreach($row as $key => $val){

        if(!is_array($val)){

            echo "$key: $val \n";
        }
        else{
             echo "Contents of $key \n";
            foreach($val as $vkey => $vval){

                if(!is_array($vval)){

                    echo "$vkey: $vval \n";
                }
                else{

                    echo "Contents of $vkey \n";
                    foreach($vval as $vvvkey => $vvvval){
                        if(!is_array($vvvval)){

                            echo "$vvvkey: $vvvval \n";
                        }
                        else{

                            echo "Contents of $vvvkey \n";
                            foreach($vvvval as $vvvvkey => $vvvvval){

                                echo "$vvvvkey: $vvvvval \n";
                            }
                        }
                    }
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.