How to extract and list values from a multidimensional array?

Asked

Viewed 8,934 times

2

I have this array:

Array
(
    [Bairro] => Array
        (
            [0] => Aberta dos Morros
            [1] => Camaquã
            [2] => Cavalhada
            [3] => Cristal
            [4] => Guarujá
            [5] => Hípica
            [6] => Hípica/Jardins do Prado
            [7] => Hípica/Lagos de Nova Ipanema
            [8] => Ipanema
            [9] => Ipanema/Altos do Ipê
            [10] => Ipanema/Jardim Verde
            [11] => Menino Deus
            [12] => Nonoai
            [13] => Terraville
            [14] => Tristeza
            [15] => Vila Assunção
            [16] => Vila Nova
        )

)

I managed to get it through this script:

$cidades = $others['resultado'];

print_r($cidades);

Now I wanted to extract each value in string form, for example:

Vila Nova simply.

Probably declare a array empty before, make a foreach and then store everything in a variable to get something like:

<a href="/Aberta dos Morros">Aberta dos Morros</a>
<a href="/Cavalhada">Cavalhada</a>
<a href="/Menino Deus">Menino Deus</a>
<a href="/Terraville">Terraville</a>
<a href="/Vila Nova">Vila Nova</a>
  • Do not understand, you just want to take the value and play on the link? or is there something else? a foreach does not solve?

  • Extracting indexes is very different from what you really want to do. So I edited the question and the title.

3 answers

5


Loop inside your array, something like:

<?php
$cidades = seu array;
for ( $a=0; count($cidades)<$a; $a++ ) {
echo $cidades['Bairro'][$a];
}

1

It would be something like that you want?

      $query = mysql_query("SELECT * FROM cidade");
         while($cidade = mysql_fetch_array($query)) { ?>
            <a href="<?php echo $cidade['id'] ?>"><?php echo $cidade['cidade'] ?></a>
  • It’s not a database. All the data I need is inside $cidades and represents what is just above this variable.

  • 1

    The logic is the same, I only put the database search and while, to better exemplify the array. Where $city['id'] is the zero position containing our id and $city['city'] is the position 1 containing the name of our city. Put your array in a while along with your code that will have the result hopefully, any doubt do not hesitate to ask.

0

I got the notes from the comic book

    $info = ldap_get_entries($connect, $search);

After I made a foreach to take the hints I wanted and put in another array:

$empresaSemDescricao = array(
        'resultado' => array()
    );
foreach ($info as $inf){
$empresaSemDescricao['resultado'][] = array(
                                'nome'      => $nome,
                                'email'     => $email,
                                'empresa'   => $empresa
                            );
}

Then you get access like this:

//Acessar o array que está dentro do array com o sindice 'resultado'
        for($a=0; $a < count($empresaSemDescricao['resultado']); $a++){
            $mensagem .= '    <tr>';
            $mensagem .= '      <td>'.$empresaSemDescricao['resultado'][$a]['nome'].'</td>';
            $mensagem .= '      <td>'.$empresaSemDescricao['resultado'][$a]['email'].'</td>';
            $mensagem .= '      <td>'.$empresaSemDescricao['resultado'][$a]['empresa'].'</td>';
            $mensagem .= '    </tr>';
        }

Browser other questions tagged

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