D
D
Darth Vader2015-03-05 07:05:32
PHP
Darth Vader, 2015-03-05 07:05:32

How to loop through arrays using foreach?

Hello, there is an array $item, when you print the array, there is such data

[pictures] => Array
        (
            [0] => Array
                (
                    [url] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1qn8UHpXXXXcmXXXXXXXXXXXX_!!0-item_pic.jpg
                    [ismain] => true
                    [small] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1qn8UHpXXXXcmXXXXXXXXXXXX_!!0-item_pic.jpg_100x100.jpg
                    [medium] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1qn8UHpXXXXcmXXXXXXXXXXXX_!!0-item_pic.jpg_310x310.jpg
                    [large] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1qn8UHpXXXXcmXXXXXXXXXXXX_!!0-item_pic.jpg_600x600.jpg
                )

            [1] => Array
                (
                    [url] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1WyHEGVXXXXbvXFXXXXXXXXXX_!!0-item_pic.jpg
                    [ismain] => false
                    [small] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1WyHEGVXXXXbvXFXXXXXXXXXX_!!0-item_pic.jpg_100x100.jpg
                    [medium] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1WyHEGVXXXXbvXFXXXXXXXXXX_!!0-item_pic.jpg_310x310.jpg
                    [large] => http://img02.taobaocdn.com/bao/uploaded/i2/TB1WyHEGVXXXXbvXFXXXXXXXXXX_!!0-item_pic.jpg_600x600.jpg
                )

            [2] => Array
                (
                    [url] => http://img04.taobaocdn.com/bao/uploaded/i4/1852243062/TB2pPS1bFXXXXcuXXXXXXXXXXXX_!!1852243062.jpg
                    [ismain] => false
                    [small] => http://img04.taobaocdn.com/bao/uploaded/i4/1852243062/TB2pPS1bFXXXXcuXXXXXXXXXXXX_!!1852243062.jpg_100x100.jpg
                    [medium] => http://img04.taobaocdn.com/bao/uploaded/i4/1852243062/TB2pPS1bFXXXXcuXXXXXXXXXXXX_!!1852243062.jpg_310x310.jpg
                    [large] => http://img04.taobaocdn.com/bao/uploaded/i4/1852243062/TB2pPS1bFXXXXcuXXXXXXXXXXXX_!!1852243062.jpg_600x600.jpg
                )

            [3] => Array
                (
                    [url] => http://img03.taobaocdn.com/bao/uploaded/i3/1852243062/TB2fWFZbpXXXXa_XXXXXXXXXXXX_!!1852243062.jpg
                    [ismain] => false
                    [small] => http://img03.taobaocdn.com/bao/uploaded/i3/1852243062/TB2fWFZbpXXXXa_XXXXXXXXXXXX_!!1852243062.jpg_100x100.jpg
                    [medium] => http://img03.taobaocdn.com/bao/uploaded/i3/1852243062/TB2fWFZbpXXXXa_XXXXXXXXXXXX_!!1852243062.jpg_310x310.jpg
                    [large] => http://img03.taobaocdn.com/bao/uploaded/i3/1852243062/TB2fWFZbpXXXXa_XXXXXXXXXXXX_!!1852243062.jpg_600x600.jpg
                )

            [4] => Array
                (
                    [url] => http://img01.taobaocdn.com/bao/uploaded/i1/1852243062/TB2fADTbVXXXXbZXXXXXXXXXXXX_!!1852243062.jpg
                    [ismain] => false
                    [small] => http://img01.taobaocdn.com/bao/uploaded/i1/1852243062/TB2fADTbVXXXXbZXXXXXXXXXXXX_!!1852243062.jpg_100x100.jpg
                    [medium] => http://img01.taobaocdn.com/bao/uploaded/i1/1852243062/TB2fADTbVXXXXbZXXXXXXXXXXXX_!!1852243062.jpg_310x310.jpg
                    [large] => http://img01.taobaocdn.com/bao/uploaded/i1/1852243062/TB2fADTbVXXXXbZXXXXXXXXXXXX_!!1852243062.jpg_600x600.jpg
                )

        )

So here's how I iterate over the data using foreach to display a picture [small].
I can only display the first image in this way, for example. But manually displaying each image this way is just tough, since the number of images for different products can be different. So how should I be?
<?=$item['pictures'][0] ['small']?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-03-05
@blackdarthvader

foreach($item['pictures'] as $value) {
     echo $value['small'];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question