Php json output - what am i doing wrong?


probably something simple but i am not seeing it.

I have a simple json file that is outputting correctly when viewed directly.

I want to call in this data though and output on a php page so i’ve written this

$jsonitem = file_get_contents("conundrum.json");

$objitems = json_decode($jsonitem);

//output links to all the images for this beach
$row_set = count($objitems);
//lets check how many it is finding
echo $row_set.'<br>';

//loop through and print out
for ($row = 0; $row < $row_set; $row++)
			if($row !==''){
				echo 'Tip title ';
                echo $objitems['tip'];
				echo '<br>';

It is correctly outputting ‘2’ as the number of results and echo’ing 2 lots of tip title so it is looping through. so it must be the variable echo $objitems[‘tip’];

What am i doing wrong? I’ve tried appending the variable with an ID number but that didn’t work either.

Json looks like this




          "tip":"Make up",

          "date":"11th June 2017",


          "description":"Apart from not using any (which I do mostly), are there ANY ways to go plastic free with make up?! Like powder, mascara and foundation?",

          "category":"In the bathroom"





          "date":"2nd June 2017",


          "description":"Anyone got any clever toothpaste ideas? It all seems to be in plastic tubes these days!",

          "category":"In the bathroom"



any help appreciated. thanks

I think you’re using the wrong loop function. I’m not on my laptop to test it out, but I assume it’s because you’re using for loop instead of foreach.

found the problem. I didn’t enable converting to associative arrays and i needed to add an identifier for the row in the loop.


`// pass true to convert objects to associative arrays
$book = json_decode($json, true);
// access title of $book array
echo $book[‘title’]; // JavaScript: The Definitive Guide

Just need to add true

$objitems = json_decode($jsonitem, true);

then when outputting

echo $objitems[$row]['tip'];

Then it works.

Or it turns out i could leave it and just access like this

echo $objitems[$row]->tip;

They are useful but my brain doesn’t like working with arrays :sweat_smile:


1 Like

ok next question. Might be better as a separate thread not sure but i’ll post here for now.

So i want to add a sub-array for comments associated with each tip. I’ve successfully done this but now i want to be able to filter the top level tips. Again i can get this to work but with one bit not quite working.

If no GET vaiable is set i want it to loop through like this (which works)

 if($row !=='' ){

if a GET variable is set i need this (which also works)

 if($row !=='' && $objitems[$row]['category']== $_GET['filter'] ){

But what i can’t do is combine those so it returns all of them if no get variable and filters if it is set.

I could do an if/else and copy all of the code into each of them but that seems like duplication when i’m sure there is a way of combining.

Hopefully i’ve explained it correctly.


I’d probably use the ternary operator there:

if ($row != '' && (isset($_GET['filter'])? $objitems[$row]['category'] == $_GET['filter'] : true)) { 
                        conditional                        return this if true            otherwise this

If I’ve got the brackets correct, that would check to see whether your $_GET variable is set, and if it is, it will return the value of the comparison between the $_GET variable and the object category field. If it is not set, it will return TRUE anyway.

(Note: there are probably much neater ways to do this - probably just splitting that part into a separate line inside your for loop to create a true or false variable would make it easier to read).

What I don’t understand is this check

if ($row != ''

because (unless you’ve changed the code and not mentioned it) that is inside a loop

for ($row = 0; $row < $row_set; $row++)

I can’t see a circumstance when $row will ever be an empty string.

1 Like

oh yeah i think the ‘if ($row != ‘’’ was probably left over from a piece of code i was using before. Now you point it out it is obvious it’s not needed. doh.

I’ll give the top bit of code you’ve put a go. It’s looks like what i was trying to achieve, I’d tried something similar but couldn’t get it to work as my knowledge was lacking.

I’ve just got to nip out for an hour so will give it a go when i’m back.

many thanks :slight_smile:

brilliant all working. I made a few amends as when i set a form up to filter it i realised it could send an empty variable for the ‘please select’ option which would return no results, so i just changed it to

if (isset($_GET['filter']) && ($_GET['filter'] !=='')? $objitems[$row]['category'] == $_GET['filter'] : true) {

working perfectly now.

thanks you saved me a lot of head scratching and/or duplicated code in an if/else.

I don’t know if this matters, but I rather use the word keywords since they are simplier to work with. Sometimes I confuse && with “or” and vice versa. So I often do if(... OR ...) or else if(... AND ...)

Weirdly i tend to prefer it as && etc in if/else type queries. although having said that i prefer words in sql queries. No idea why probably just the way i learnt it at some point and i haven’t broken the habit.

thanks though it was worth mentioning just in case as others may find it easier too.

1 Like

One thing you may want to keep in mind is operator precedence. Probably not an issue in most cases (unless there is a mix of both being used), but “&&” has precedence over “and


that’s certainly worth knowing. I assumed they were one and the same. I tend to only use one type and stick to it in a query but it’s always that odd time when for some reason i do something different and can’t work out why it isn’t working.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.