Read Array Multidimensional PHP [duplicate]

3

I need to read an array and get the name of its 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 only the values are printed, I also need the name of the column, for example: Conta = BEATRIX

    
asked by anonymous 17.11.2017 / 19:58

3 answers

5

You have to use a foreach that you can redeem, including the example

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

where $r is the index and $k is the value , but in your example you have to create two foreach because it is a array containing% and to search for the indexes of array more internal, use the above technique, eg:

<?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

17.11.2017 / 20:02
2

In foreach use the $key => $value syntax this will return the repectives key name and value.

change:

foreach ($Array as $row)

To:

 foreach ($Array as $chave => $row)
    
17.11.2017 / 20:03
2

You just need to give a modified one on your for

foreach ($Array as $key => $row)
{
    // seu codigo
}

where the variable $key has the value of the array id, I already answered a similar question here the link: How to print one array with indexes and values in PHP?

    
17.11.2017 / 20:04