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'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?