Take array value

Asked

Viewed 1,262 times

0

I need to get the values of the array where you have the photos. I managed to get what you name, but the photos I’m hitting head...rs

Array

Array
(
    [0] => Array
        (
            [0] => Teste 8
            [1] => Teste b
        )

    [1] => Array
        (
            [0] => avaria_20190614005447368417.jpg
            [1] => avaria_20190614005450305809.jpg
        )

)

php

<?php
  $avariasIni = unserialize($row_rsRegistros['avariasIni']);

  foreach($avariasIni[0] as $avaIni => $avaKey) {
    echo $avaKey ." = ". $avaIni ."<br />";
  }
?>

The variable I’m taking the name is $avaKey, now I need to get the name of the photo

4 answers

4


You are using a multidimensional array. If the two arrays have the same amount of items, you can use the loop as follows.

for ($i = 0; $i < count($avariasIni[0]); $i++) {
    echo $avariasIni[0][$i] ." = ". $avariasIni[1][$i] ."<br />";
}

//Saída
//Teste 8 = avaria_20190614005447368417.jpg
//Teste b = avaria_20190614005450305809.jpg
  • Victor, it is possible to do with the foreach?

  • If you can do it with foreach for me it’s better.

  • @Tiago, I’ll put one Edit to update the response :)

  • @Tiago, each value of an array references the value of the other array?

4

As I have shown in Mount two-dimensional PHP array, the function array_map already makes this association for you:

$dados= [
    ['Teste 8', 'Teste B'],
    ['avaria_20190614005447368417.jpg', 'avaria_20190614005450305809.jpg']
];

$agrupado = array_map(function ($nome, $imagem) {
    return compact('nome', 'imagem');
}, $dados[0], $dados[1]);

var_export($agrupado);

The exit would be:

array (
  0 =>
  array (
    'nome' => 'Teste 8',
    'imagem' => 'avaria_20190614005447368417.jpg',
  ),
  1 =>
  array (
    'nome' => 'Teste B',
    'imagem' => 'avaria_20190614005450305809.jpg',
  ),
)

Just iterate and do what you want:

foreach ($agrupado as $dado) {
  echo "{$dado['nome']} - {$dado['imagem']}", PHP_EOL;
}

Generating the output:

Teste 8 - avaria_20190614005447368417.jpg
Teste B - avaria_20190614005450305809.jpg

2

Well, it seems that you have a multidimensional array where the indexes of one relate to the content of the other, so you can start organizing it (ideally it would be already organized where you treat the sending/query of these values).

I created an array with the same structure as yours for testing.

$avariasIni = array(
    array('Teste 8', 'Teste B'),
    array('avaria_20190614005447368417.jpg', 'avaria_20190614005450305809.jpg'));

With this I made a small routine to return the organized array where you can access and manipulate as you want.

foreach($avariasIni as $key => $avarias){
    if($key == 0){
        foreach($avarias as $key2 => $nome){
            $temp[$key2]['nome_foto'] = $nome;
        }
    }
    if($key == 1){
        foreach($avarias as $key2 => $foto){
            $temp[$key2]['link_foto'] = $foto;
        }
    }

}

Upshot:

array(2) {
  [0]=>
  array(2) {
    ["nome_foto"]=>
    string(7) "Teste 8"
    ["link_foto"]=>
    string(31) "avaria_20190614005447368417.jpg"
  }
  [1]=>
  array(2) {
    ["nome_foto"]=>
    string(7) "Teste B"
    ["link_foto"]=>
    string(31) "avaria_20190614005450305809.jpg"
  }
}

1

If you know the length of the arrays and/or want to access only one specific item, you can access it directly by its index.

$avariasIni = unserialize($row_rsRegistros['avariasIni']);

// printa o nome e o arquivo do primeiro item

print $avariasIni[0][0]; // nome do primeiro arquivo
print $avariasIni[1][0]; // nome do arquivo do primeiro item

print $avariasIni[1][0]; // nome do segundo arquivo
print $avariasIni[1][1]; // nome do arquivo arquivo do segundo item

// restante do codigo aqui...

Browser other questions tagged

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