PHP get value from a multidimensional array

0
 $arquivo = '{"nome":"João","cpf":"00000","teste":"ooooo"}';
 $dd = json_decode($arquivo, TRUE);

foreach($dd as $value)
{
$nome = $value->{'nome'};
$cpf = $value->{'cpf'};

}

I want to get the value of name and cpf from the array but the result comes null to the variables, does anyone know how to solve it? please

    
asked by anonymous 22.01.2017 / 19:36

1 answer

4

When you use the json_decode function, it converts the json data into an associative array, so you can use the foreach by calling an element of a vector using an index and not an object's attributes. For example:

<?php
$arquivo = '[{"nome":"João","cpf":"00000","teste":"ooooo"},{"nome":"João 2","cpf":"000002","teste":"ooooo2"}]';
$dd = json_decode($arquivo, TRUE);
foreach($dd as $value){
    echo $value['nome'].'<br/>';
    echo $value['cpf'].'<br/>';
    echo $value['teste'].'<br/>';
}
?>

In the example I added one more json element, so you have two records to help you understand. I hope I have helped.

    
22.01.2017 / 19:56