如何读取包含url的json文件并保存url

时间:2013-09-16 02:46:29

标签: php json

我有一个json文件。 json文件包含一些图像的路径。我想读取url路径并将图像保存在我的计算机中。我能够读取json文件但无法获取json中的image_path对象。我的json文件格式为:

Array
(
    [0] => Array
        (
            [product] => Array
                (
                    [product_id] => 262
                    [product_name] => VD0289 CUT OUT BACK DRESS
                    [product_inventory] => 2
                    [product_price] => B
                    [label_name] => 15
                    [product_status] => A
                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [image_id] => 935
                                    [image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289.jpg
                                    [image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289.jpg
                                    [image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289.jpg
                                    [image_type] => M
                                )

                            [1] => Array
                                (
                                    [image_id] => 938
                                    [image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289_(4).jpg
                                    [image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289_(4).jpg
                                    [image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289_(4).jpg
                                    [image_type] => A
                                )



                        )

                    [options] => Array
                        (
                        )

                )

        )

    [1] => Array
        (
            [product] => Array
                (
                    [product_id] => 263
                    [product_name] => Chic Chanel Inspired Dress - Blue
                    [product_inventory] => 1
                    [product_price] => O
                    [label_name] => 9
                    [product_status] => A
                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [image_id] => 939
                                    [image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-1.jpg
                                    [image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-1.jpg
                                    [image_detailed] => http://gird.com/images/detailed/0/100678-Blue-1.jpg
                                    [image_type] => M
                                )

                            [1] => Array
                                (
                                    [image_id] => 942
                                    [image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-4.jpg
                                    [image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-4.jpg
                                    [image_detailed] => http://gird.com/images/detailed/0/100678-Blue-4.jpg
                                    [image_type] => A
                                )

                         ..............................

如何从此文件中获取所有图像路径和image_detailed?我的PHP代码是:代码只是获取第一个图像路径。

<?php

$file="dw.json";

$json=  json_decode(file_get_contents($file),true);
print_r ($json[0]["product"]["images"][0]["image_path"]);
//print_r($json);
?>

2 个答案:

答案 0 :(得分:1)

试试这个:

foreach($json as $key => $products) {
$product = $products['product'];
    foreach($product['images'] as $key => $image) {
        $product_images[$product['product_name']][] = $image['image_path'];
    }
}

print_r($product_images);

答案 1 :(得分:0)

使用for循环,如下所示:

foreach ($json[0]["product"]["images"] as $key)
    echo $key['image_path'];