如何显示multidiamentional数组中的值

时间:2017-10-23 12:08:07

标签: php arrays multidimensional-array foreach

这里我有一个数组,从这里我希望显示所有值,但不知道如何从这个数组获取所有值,我是开发的新人任何一个知道意味着请更新我的代码。我花了很多时间但我无法找到确切的答案

print_r($planMaster);

{
"status": "success",
"message": "Total 3 record(s) found.",
"total_record": 3,
"data": [
    {
        "planId": "1",
        "planName": "Easy Plan",
        "createdOn": "2017-10-23 15:17:08",
        "createdBy": "Kani",
        "updatedOn": "0000-00-00",
        "updatedBy": "",
        "planDescription": [
            {
                "descriptionId": "1",
                "planId": "1",
                "plandescTitle": "Contacts details up to",
                "plandescResult": "24"
            },
            {
                "descriptionId": "2",
                "planId": "1",
                "plandescTitle": "Bonus Contact details up to",
                "plandescResult": "2"
            },
            {
                "descriptionId": "3",
                "planId": "1",
                "plandescTitle": "Area Master Assistance",
                "plandescResult": "No"
            },
            {
                "descriptionId": "4",
                "planId": "1",
                "plandescTitle": "Contact details through sms",
                "plandescResult": "Yes"
            }
        ],
        "planValue": [
            {
                "pvId": "2",
                "planId": "1",
                "startDate": "2017-10-23",
                "endDate": "2017-10-30",
                "planValue": "1000"
            }
        ]
    },
    {
        "planId": "2",
        "planName": "Cool Plan",
        "createdOn": "2017-10-23 16:58:32",
        "createdBy": "Kani",
        "updatedOn": "0000-00-00",
        "updatedBy": "",
        "planDescription": [
            {
                "descriptionId": "5",
                "planId": "2",
                "plandescTitle": "Contact details up to",
                "plandescResult": "48"
            },
            {
                "descriptionId": "6",
                "planId": "2",
                "plandescTitle": "Bonus Contact details up to",
                "plandescResult": "4"
            },
            {
                "descriptionId": "7",
                "planId": "2",
                "plandescTitle": "Area Master Assistance",
                "plandescResult": "Yes"
            },
            {
                "descriptionId": "8",
                "planId": "2",
                "plandescTitle": "Contact details through sms",
                "plandescResult": "Yes"
            }
        ],
        "planValue": [
            {
                "pvId": "3",
                "planId": "2",
                "startDate": "2017-10-23",
                "endDate": "2017-10-30",
                "planValue": "2000"
            }
        ]
    },
    {
        "planId": "3",
        "planName": "Free Plan",
        "createdOn": "2017-10-23 17:09:20",
        "createdBy": "Kani",
        "updatedOn": "0000-00-00",
        "updatedBy": "",
        "planDescription": [
            {
                "descriptionId": "9",
                "planId": "3",
                "plandescTitle": "Contacts details up to",
                "plandescResult": "5"
            },
            {
                "descriptionId": "10",
                "planId": "3",
                "plandescTitle": "Area Master Assistance",
                "plandescResult": "No"
            },
            {
                "descriptionId": "11",
                "planId": "3",
                "plandescTitle": "Contact details through sms",
                "plandescResult": "Yes"
            }
        ],
        "planValue": [
            {
                "pvId": "4",
                "planId": "3",
                "startDate": "2017-10-23",
                "endDate": "2017-10-30",
                "planValue": "0"
            }
        ]
    }
]
}

预期答案

Plan Name: Easy Plan


    plandescTitle: Contacts details up to
     plandescResult: 24

     plandescTitle: Bonus Contact details up to
     plandescResult": 2

我试过这样但没有得到答案

 <?php
      $planMaster =  GetResponse($API_URL.'getplan_Master');
      if (!empty($planMaster)) {                           
      foreach ($planMaster['data'] as $result) {
      ?>  
          <th><?php echo $result['planName']; ?> 1000/-</th>


            <?php
            foreach ($planMaster['planDescription'] as $descresult) {
                echo $descresult['plandescTitle'];
            }

            ?>

     <?php
      }
      }
    ?>

2 个答案:

答案 0 :(得分:0)

试试这个。我会解释它是否有效。

 <?php
      $planMaster =  GetResponse($API_URL.'getplan_Master');
      if (!empty($planMaster)) {                           
      foreach ($planMaster['data'] as $result) {
      ?>  
          <th><?php echo $result['planName']; ?> 1000/-</th>


            <?php
            foreach ($result['planDescription'] as $descresult) {
                echo $descresult['plandescTitle'];
            }

            ?>

     <?php
      }
      }
    ?>

答案 1 :(得分:0)

你需要使用json_decode()函数将json数据转换为php Array

 $planMaster =  GetResponse($API_URL.'getplan_Master');
 $planMasterArray = json_decode($planMaster, true);
 if (isset($planMasterArray) && is_array($planMasterArray)) {                           
    foreach ($planMasterArray['data'] as $result) {
    ?>  
      <th><?php echo $result['planName']; ?> 1000/-</th>
        <?php
        foreach ($planMaster['planDescription'] as $descresult) {
            echo $descresult['plandescTitle'];
        }
        ?>
   <?php
    }
  }
?>