How to extract a given index within this array with a for?

Asked

Viewed 116 times

2

I’d like to extract all the clues [Foto] of this array but still do not understand correctly how to do this as I am still struggling with learning array multidimensionais. What I was able to do was extract 1 photo thus:

echo $fotos = $result['Foto'][1]['Foto'];

Question: Like inside a for I can extract all contents [Foto]?

The array is this:

Array
(
    [Codigo] => 67
    [Foto_Codigo] => 7
    [Foto] => Array
        (
            [1] => Array
                (
                    [Codigo] => 1
                    [Foto] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311964c945c.jpg
                    [FotoPequena] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311964c945c_p.jpg
                    [Destaque] => Sim
                )

            [2] => Array
                (
                    [Codigo] => 2
                    [Foto] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311963961c5.jpg
                    [FotoPequena] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311963961c5_p.jpg
                    [Destaque] => Nao
                )

            [3] => Array
                (
                    [Codigo] => 3
                    [Foto] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311964e6f0b.jpg
                    [FotoPequena] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311964e6f0b_p.jpg
                    [Destaque] => Nao
                )

            [4] => Array
                (
                    [Codigo] => 4
                    [Foto] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311965dd324.jpg
                    [FotoPequena] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311965dd324_p.jpg
                    [Destaque] => Nao
                )

            [5] => Array
                (
                    [Codigo] => 5
                    [Foto] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311965cc92b.jpg
                    [FotoPequena] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_6756311965cc92b_p.jpg
                    [Destaque] => Nao
                )

            [6] => Array
                (
                    [Codigo] => 6
                    [Foto] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_67563119654e991.jpg
                    [FotoPequena] => http://static.vista.s3.amazonaws.com/danielbo/vista.imobi/fotos/67/ik5u4v_67563119654e991_p.jpg
                    [Destaque] => Nao
                )

1 answer

1


You can do it in several different ways, all with the same result:

With foreach:

In this case we dispensed with the index of each photo and have already received directly the sub-array:

$fotos = [];
foreach ($array['Foto'] as $foto) {
    $fotos[] = $foto['Foto'];
}

With for:

The only caveat in that code is that the initial index is 1, and not 0; so we need to generate a new array whose indexes begin with 0, so that we can travel it without problems in the for:

$fotosAnterior = array_values($array['Foto']);
$fotos = [];
for ($i=0; $i<count($fotosAnterior); $i++) {
    $fotos[] = $array['Foto'][$i]['Foto'];
}

With array_map:

Shorter code, but a little harder to understand for beginners. The idea is to map each item of the array from photos to a return - in this case, the item’s own value Foto:

$fotos = array_map(function(array $foto) {
    return $foto['Foto'];
}, $array);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.