Read PHP Multidimensional Array

Asked

Viewed 7,570 times

3

I need to read an array and get the name of your column and its value.

Array:

Array
(
    [0] => Array
        (
            [Conta] => FRANCIELE OLIVEIRA
            [CPF] => ''
            [Telefone Res.] => (00) 0000-0000
        )

    [1] => Array
        (
            [Conta] => BEATRIX BEHN
            [CPF] => ''
            [Telefone Res.] => (00) 0000-0000
        )
)


foreach ($Array as $row)
{
    echo $row['Conta'];
    echo $row['CPF'].'<BR><BR>';
}

But with this foreach I only print the values, I also need the column name, for example: Conta = BEATRIX

3 answers

5

You have to use a foreach which can even redeem the Internet, example:

foreach ($array as $r => $k)
{        
}

where the $r is the index and the $k is the value respective, only that in your example you have to create two foreach, because it’s a array that contain array and to search the contents of array more internal, use the above technique, example:

<?php

$Array = array(
    array("Conta"=>"FRANCIELE OLIVEIRA", "CPF"=>"","Telefone Res."=>'(00) 0000-0000'),
    array("Conta"=>"BEATRIX BEHN", "CPF" => "","Telefone Res."=>'(00) 0000-0000')
);

foreach ($Array as $row)
{
    foreach($row as $i => $a)
    {
        echo '<div>'. $i." ".$a .'</div>';
    }
}

Online Example

References:

2

In foreach use the syntax $key => $value This will return the reflective key name and value.

change:

foreach ($Array as $row)

To:

 foreach ($Array as $chave => $row)

2

Browser other questions tagged

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