PHP is not displaying array result

0

I'm with the code:

<?php

$d = json_decode($output);

?>

The $ output variable sends jSON data to PHP. For you to understand this variable better, 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 loop the data returned. I have display doing

<?php

echo $d['nome_1'];

?>

Only the screen turns white, nothing appears. I actually wish that instead of having _1, _2 all be [name], [cpf] etc ... and I would looping later. 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);
?>
    
asked by anonymous 27.07.2014 / 22:57

3 answers

5

You have two problems, not just one.

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

  

Fatal error: Can not 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 for your echo .

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

During development it is a 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 are most interested in, is why it did not work as you would expect it to.

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

Although the objects in this class are iterable, just like a conventional array, just like any object (provided it meets some requirements that are not in the topic), and any object can also be accessed by bracket notation in an array, irony, stdClass can not be.

However, the json_decode () provides an alternative for instead of returning an object the information is structured in an array simple, simply by saying a second argument like TRUE :

$data = json_decode( $output, TRUE );

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

$data = (array) json_decode( $json );
    
27.07.2014 / 23:34
4

Use json_decode with second argument set to true :

$d = json_decode($output, true);

It will return an associative array and you can iterate it.

    
27.07.2014 / 23:30
0

Return is an object, you already solved another solution would be to use a foreach to go through it.

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