dousi1994 2019-04-09 01: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 01: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 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部