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

2

I would like to extract all the [Foto] indices from this array but I still do not understand correctly how to do this because I am still struggling with array multidimensionais learning. What I was able to do was extract 1 photo like this:

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

Question: As within for can I extract all indexes [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
                )
    
asked by anonymous 25.11.2015 / 16:35

1 answer

1

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

With foreach :

In this case, we will dispense with the index of each photo and we have already received the sub-array :

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

With for :

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

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

With array_map :

Code shorter, but a little more difficult to understand for beginners. The idea is to map each item in the array to a return - in this case, the value of the item Foto :

$fotos = array_map(function(array $foto) {
    return $foto['Foto'];
}, $array);
    
25.11.2015 / 16:47