检索和更改多维数组中的项目



我在PHP中有一个表示车辆的数组:

    Array ( 
    [0] => Array ( 
        [VehicleClass] => Car 
        [IsNew] => 1 
        [Year] => 2016 
        [Make] => Cadillac 
        [Model] => ATS 
        [ModelCode] => 6AC69 
        [Trim] => 2.0L Turbo Luxury Collection 
        [Headline] => 2016 Cadillac ATS Luxury 
        [ImageList] => Array ( 
            [0] => http://example.com/01e8c76c1339f342837e7f085fa5984559849e54_large.jpeg 
            [1] => http://example.com/ef86b6a54888afa29d3bab8df74ee4d520c1b0cd_large.jpeg 
            [2] => http://example.com/50437892/c0c5177c707ac2fbf3a1bb087a067b51bfe4427b_large.jpeg 
            [3] => http://example.com/49067490d1ee673831df15413784cb1bb35b4142_large.jpeg 
            [4] => http://example.com/de12530a19cd38dd07942a8f3947debb4a64b0cf_large.jpeg 
        )
    )
)
    Array ( 
    [1] => Array ( 
        [VehicleClass] => Car 
        [IsNew] => 1 
        [Year] => 2016 
        [Make] => Cadillac 
        [Model] => ATS 
        [ModelCode] => 6AC69 
        [Trim] => 2.0L Turbo Luxury Collection 
        [Headline] => 2016 Cadillac ATS Luxury 
        [ImageList] => Array ( 
            [0] => http://example.com/01e8c76c1339f342837e7f085fa5984559849e54_large.jpeg 
            [1] => http://example.com/ef86b6a54888afa29d3bab8df74ee4d520c1b0cd_large.jpeg 
            [2] => http://example.com/50437892/c0c5177c707ac2fbf3a1bb087a067b51bfe4427b_large.jpeg 
            [3] => http://example.com/49067490d1ee673831df15413784cb1bb35b4142_large.jpeg 
            [4] => http://example.com/de12530a19cd38dd07942a8f3947debb4a64b0cf_large.jpeg 
        )
    )
)

我正在尝试访问ImageList数组,检索URL以下载图像,然后更改URL。我尝试了很多不同的东西,包括这个网站上的许多想法,但我觉得我错过了一些简单的东西。

外部阵列为$aVehicle。因此,上面例子中的每个车辆都由$aVehicle[0]和$aVehicle[1]表示。不过,我需要循环浏览数百个。

谢谢你的帮助!

我尝试过的东西:

 foreach ($aVehicle as $vehicle => $data) {
    if(is_array($data)) {
        foreach ($data as $title => $value) {
            if(is_array($value)) {
                foreach ($title['ImageList'] as $imageNum => $imageURL) {
                    $imageURL = 'newURL.com';   
                }
            }           
        }
    }
}

foreach ($aVehicle as $vehicle => $data) {
    foreach ($data['ImageList'] as $imageURL) {
        $imageURL = 'newURL.com';   
    }
}

现在这个

我可以访问数组中的值,但无法更改它。

foreach ($aVehicle as $vehicle => $data) {
    foreach ($data as $title => $value) {
        if ( $title == 'ImageList' ) {
            foreach ($value as &$imageURL) {
                $imageURL = "newURL";
            }
        }       
    }
}

我自己设法解决了这个问题。请参阅以下代码:

foreach ($aVehicle as $key => $data) {
    foreach ($data as $subkey => $value) {
        if ( $subkey == 'ImageList' ) {
            foreach ($value as $subsubkey => $imageURL) {
                $aVehicle[$key][$subkey][$subsubkey] = 'newURL.com';
            }
        }       
    }
}

相关内容

  • 没有找到相关文章

最新更新