dousi1994 2019-04-09 09:28
浏览 58
已采纳

如何使用foreach从多维数组创建变量

I'm trying to create variables from a multidimensional array using foreach, but cannot access the nested arrays.

I tried using foreach again within my current foreach command, but I think something in the format of the array will not let me do this.

PHP:

$response = curl_exec($cURL);

$result = json_decode($response, true);

$someArray = $result;
foreach ($someArray as $key => $value) {;
}

curl_close($cURL);

print $value["registration"];
print $value["make"];
print $value["model"];
print $value["fuelType"];
print $value["primaryColour"];

This is exactly how I receive the array. I can create variables for registration, make, model, firstUsedDate, fuelType and primaryColour.

array(
    [0] => array(
        [registration]  => DU11LVA
        [make]          => MINI
        [model]         => MINI(R57)
        [firstUsedDate] => 2011.03.05
        [fuelType]      => Diesel
        [primaryColour] => White

        [motTests]      => array(

            [0] => array(
                [completedDate]  => 2018.04.1116:00:30
                [testResult]     => PASSED
                [expiryDate]     => 2019.04.10
                [odometerValue]  => 45283
                [odometerUnit]   => mi
                [motTestNumber]  => 138359719457

                [rfrAndComments] => array(
                    [0] => array(
                        [text] => Nearside Front Road wheel with a slightly distorted bead rim INSIDE (4.2.A.1a) 
                        [type] => ADVISORY)))

            [1] => array(
                [completedDate]  => 2018.04.1110:22:54
                [testResult]     => FAILED
                [odometerValue]  => 45283
                [odometerUnit]   => mi
                [motTestNumber]  => 652014545840

                [rfrAndComments] => array(
                    [0] => array(
                        [text] => Nearside Front Road wheel with a slightly distorted bead rim INSIDE (4.2.A.1a) 
                        [type] => ADVISORY)
                    [1] => array(
                        [text] => Nearside Front Shock absorber has a serious fluid leak (2.7.3) 
                        [type] => FAIL)))

            [2] => array(
                [completedDate]  => 2017.04.0610:27:08
                [testResult]     => PASSED
                [expiryDate]     => 2018.04.08
                [odometerValue]  => 37579
                [odometerUnit]   => mi
                [motTestNumber]  => 605834707501

                [rfrAndComments] => array())

I'm trying to get the expiryDate and odometerValue.

  • 写回答

1条回答 默认 最新

  • douhe3313 2019-04-09 09:42
    关注

    You have an array that contains other arrays, so keeping it simple and just using foreach loops, when ever you want something from an inner array you start another foreach loop to process it.

    Also the code that is making use of the loop processing should live inside the foreach and not be coded after it.

    $response = curl_exec($cURL);
    
    $result = json_decode($response, true);
    curl_close($cURL);
    
    foreach ($result as $value) {    
    
        print $value["registration"];
        print $value["make"];
        print $value["model"];
        print $value["fuelType"];
        print $value["primaryColour"];
    
        foreach ($value['motTests'] as $mot) {
            print $mot['expiryDate'];
            print $mot['odometerValue'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)