I need to display only one part of the encoded JSON object

Hi, I am new to PHP, but I succeeded:

I am trying to display only one part of a decoded JSON object. I called the $Results object.

I can successfully use var_dump ($Results); and then get the full results as follows:

 object(stdClass)[2] public '0' => object(stdClass)[3] public 'forename_1' => string 'JAMES' (length=5) public 'middle1_1' => string '' (length=0) public 'middle2_1' => string '' (length=0) public 'middle3_1' => string '' (length=0) public 'surname_1' => string 'TURNER' (length=7) public 'Status' => int 100 

Then I insert this into the table using the following code:

 <html> <form id="client-details" action="/details.php" method="post"> <table> <thead> <tr> <th>First Name</th> <th>Surname</th> <th>Search</th> </tr> </thead> <?php foreach($Results as $o):?> <tr> <td id="forename"><?= $o->forename_1 ?></td> <td id="surname"><?= $o->surname_1 ?></td> <td><button type="submit" >More Info</button></td> </tr> <?php endforeach; ?> </table></form> </html> 

There is a problem. When I show the results, I get the following error: "Note: attempt to get a non-object property .."

It looks like I'm trying to start the public 'Status' => int 100 object.

So my question is: How can I either stop the table from trying to fill out this "status", or how can I completely ignore it?

EDIT: If I wanted to be able to get the results from json_decode as an associative array and not as objects ... would this help me ignore the "status" array / object?

+6
source share
4 answers

I think you got it wrong. What you do is iterate over all the variables of the object, i.e. Firstly, you get the public variable 0 (which is also an object), and in the second run of the foreach statement, you get the Status variable and because the value "Status" is int and does not have a property named 'forename_1', etc. you get an error that the property does not exist.

If you really want this to work, you need to restructure the JSON object so that you can iterate over the list of people you want to display, for example:

 object(stdClass)[2] public 'list' => array(0 => object(stdClass)[3] public 'forename_1' => string 'JAMES' (length=5) public 'middle1_1' => string '' (length=0) public 'middle2_1' => string '' (length=0) public 'middle3_1' => string '' (length=0) public 'surname_1' => string 'TURNER' (length=7) public 'Status' => int 100, 1 => object(stdClass)[3] public 'forename_1' => string 'JAMES' (length=5) public 'middle1_1' => string '' (length=0) public 'middle2_1' => string '' (length=0) public 'middle3_1' => string '' (length=0) public 'surname_1' => string 'TURNER' (length=7) public 'Status' => int 100, 2 => object(stdClass)[3] public 'forename_1' => string 'JAMES' (length=5) public 'middle1_1' => string '' (length=0) public 'middle2_1' => string '' (length=0) public 'middle3_1' => string '' (length=0) public 'surname_1' => string 'TURNER' (length=7) public 'Status' => int 100 ) 

EDIT:

if you cannot or do not want to change the data structure, then you will get the result of calling the json_decode function as an associative array and in the foreach statement you then check if the required field exists:

 $Result = json_decode($data, true); <?php foreach($Results as $o):?> <?php if(isset($o['forename_1']) && isset($o['surname_1'])): ?> <tr> <td id="forename"><?= $o['forename_1'] ?></td> <td id="surname"><?= $o['surname_1'] ?></td> <td><button type="submit" >More Info</button></td> </tr> <?php endif; ?> <?php endforeach; ?> 
+3
source

I found an acceptable solution! By adding true inside my json_decode string:

 $Results = json_decode($serviceResponse, true); 

I return the result as an associative array instead of an object.

Then I changed the code inside each <tr> as follows:

 <html> .... (rest of code here) <?php foreach($Results as $person):?> <tr> <td id="forename"><?= $person['forename_1'] ?></td> <td id="surname"><?= $person['surname_1'] ?></td> <td><button type="submit" >More Info</button></td> </tr> <?php endforeach; ?> </table> </form> </html> 

So now it happens that the final string 'status' , returning an error, now is only an empty string. Although this is not an ideal solution, I am not opposed to dealing with an empty table row when viewing a page as HTML.

+1
source

Try the following:

 <html> <form id="client-details" action="/details.php" method="post"> <table> <thead> <tr> <th>First Name</th> <th>Surname</th> <th>Search</th> </tr> </thead> <?php foreach($Results as $o=>$v){?> <tr> <td id="forename"><?= $v->forename_1 ?></td> <td id="surname"><?= $v->surname_1 ?></td> <td><button type="submit" >More Info</button></td> </tr> <?php } ?> </table></form> </html> 
0
source

Try printing as follows:

 <?php foreach($Results as $o):?> <tr> <td id="forename"><?= $o['forename_1']; ?></td> <td id="surname"><?= $o['surname_1'] ?></td> <td><button type="submit" >More Info</button></td> </tr> <?php endforeach; ?> 
0
source

All Articles