0

我有一个 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);
?>
4

2 回答 2

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);
于 2013-09-16T02:56:03.813 回答
0

使用 for 循环,如下所示:

foreach ($json[0]["product"]["images"] as $key)
    echo $key['image_path'];
于 2013-09-16T02:54:26.420 回答