PHP is not displaying array output

Asked

Viewed 1,119 times

0

I have the code:

<?php

$d = json_decode($output);

?>

The variable $output sends json data to PHP. To better understand this variable, I will post the return of it

object(stdClass)#1 (6) {
  ["nome_1"]=>
  string(14) "Alisson Acioli"
  ["cpf_1"]=>
  string(11) "XXXXXXXXXX"
  ["nascimento_1"]=>
  string(10) "2014-07-19"
  ["nome_2"]=>
  string(14) "Alisson Acioli"
  ["cpf_2"]=>
  string(11) "XXXXXXXXXX"
  ["nascimento_2"]=>
  string(10) "2014-07-25"
}

What I want to do is make a loop with the returned data. I have show doing

<?php

echo $d['nome_1'];

?>

only that the screen turns white, nothing appears. In fact I would like to have _1, _2 be all [name], [Cpf] etc.. and I was looping after. The code that generates json is as follows:

<?php
$SQLResults = mysql_query("SELECT * FROM {$table} WHERE {$verificacao}");

                $i = 0;

                while($data=mysql_fetch_array($SQLResults)){

                    $i++;

                    foreach($this->dados["dados"]["show"] as $val){

                        $Resultados[$val.'_'.$i] = $data[$val];
                    }

                }

                echo json_encode($Resultados);
?>
  • Is json valid? is the same json as this question

  • Yes, it’s all as described.

3 answers

5


You have two problems and not just one.

The first and most important is the fact that you are seeing a white screen because you should be seeing:

Fatal error: Cannot use Object of type stdClass as array in path to file.php on line X

Obviously with the path location for your file and the line corresponding to your echo.

This means that your error alerts are either disabled or too low to display the error.

During development it is good practice to enable the display of all possible errors by starting the script with:

ini_set( 'display_errors', TRUE );

error_reporting( E_ALL ); // Caso você tem qualquer versão do PHP que não a 5.3.x

error_reporting( E_ALL | E_STRICT ); // Especificamente para o PHP 5.3.x

The second problem, which is what you’re most interested in, is why it doesn’t work the way you’d expect it to work.

It turns out you’re treating a object as a array. See the expression stdClass Object? So, json_decode(), by default, returns an stdClass object, which is a native PHP class.

Although the objects of this class are eternal, such as a conventional array, as well as any object (as long as it meets some requirements that are not relevant to the topic), and there are ways in which any object can also be accessed through the bracket notation of an array, by irony, the stdClass cannot be.

However, the json_decode() offers an alternative so that instead of returning an object the information is structured in a simple array, simply that a second argument such as TRUE:

$data = json_decode( $output, TRUE );

You would achieve the same result by forcing the cast from variable to array:

$data = (array) json_decode( $json );

4

Use the json_decode with the second argument set in true:

$d = json_decode($output, true);

An associative array will return to you and you can use it.

0

The return is an object, you already solved another solution would be to use a foreach to traverse it.

foreach($d as $dados){
 echo $dados->nome.' - '.$dados->cpf_1.' - etc...<br>';
} 

Browser other questions tagged

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