7 Replies - 358 Views - Last Post: 22 September 2017 - 11:13 AM Rate Topic: -----

#1 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 02:58 AM

Good morning,

I have a simple function in php which is below:

public function displayMenuType() {

        $stmt = $this->conn->query("SELECT * FROM `Type`") or die($this->conn->error);

        $json = array();
        if($stmt) {
            while($result = mysqli_fetch_all($stmt,MYSQLI_ASSOC)) {
                json_decode($result);
                $json = json_encode($result, JSON_PRETTY_PRINT);
            }
            echo "<pre>".$json."</pre>";
        } else {
            return false;
        }
    }



The problem is, the output puts my json object inside square brackets/array, like below:

[
{
"ID": "1",
"Type": "Classic Starters",
"Description": ""
},
{
"ID": "2",
"Type": "Special Starters",
"Description": ""
},
{
"ID": "3",
"Type": "Tapas Menu",
"Description": ""
},
{
"ID": "4",
"Type": "Speciality Curry Dishes",
"Description": ""
},
{
"ID": "5",
"Type": "Healthier Options",
"Description": "Using coconut oil or coconut milk, no ghee, no sugars, we present our healthier range of curries."
},
{
"ID": "6",
"Type": "Traditional Curries",
"Description": ""
},
{
"ID": "7",
"Type": "Tandoori & Grilled",
"Description": ""
},
{
"ID": "8",
"Type": "Biryanis",
"Description": ""
},
{
"ID": "9",
"Type": "Stir Fry's",
"Description": "Our new range of Stir fry\u2019s, all made with chicken tikka, served with salad & mint sauce."
}
]

Is This A Good Question/Topic? 0
  • +

Replies To: Why does json_encode put my json object inside an array [] ?

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 03:57 AM

It is an array - a collection of different objects. How else would you want to structure this data as json?
Was This Post Helpful? 0
  • +
  • -

#3 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 04:12 AM

https://www.w3school...ename=demo_file

Like that ^ No square brackets there.
Was This Post Helpful? 0
  • +
  • -

#4 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 04:19 AM

Found out its the fetch_all() which causes the array as it's of an array type. Makes sense now. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 04:24 AM

Updated working code and output to achieve what I wanted, I hope with the answer my question makes more sense:

    public function displayMenuType() {

        $stmt = $this->conn->query("SELECT * FROM `Type`") or die($this->conn->error);

        $json = array();
            while($result = mysqli_fetch_assoc($stmt)) {
                $json = json_encode($result, JSON_PRETTY_PRINT);
                echo "<pre>".$json."</pre>";
            }
    }



output:
{
"ID": "1",
"Type": "Classic Starters",
"Description": ""
}
{
"ID": "2",
"Type": "Special Starters",
"Description": ""
}
{
"ID": "3",
"Type": "Tapas Menu",
"Description": ""
}
{
"ID": "4",
"Type": "Speciality Curry Dishes",
"Description": ""
}
{
"ID": "5",
"Type": "Healthier Options",
"Description": "Using coconut oil or coconut milk, no ghee, no sugars, we present our healthier range of curries."
}
{
"ID": "6",
"Type": "Traditional Curries",
"Description": ""
}
{
"ID": "7",
"Type": "Tandoori & Grilled",
"Description": ""
}
{
"ID": "8",
"Type": "Biryanis",
"Description": ""
}
{
"ID": "9",
"Type": "Stir Fry's",
"Description": "Our new range of Stir fry\u2019s, all made with chicken tikka, served with salad & mint sauce."
}
{
"ID": "10",
"Type": "Nanbabs",
"Description": "A choice of Stirfry\u2019s made with chicken tikka, served on a fresh nan bread with mint sauce."
}
{
"ID": "11",
"Type": "Vegetarian Side Dishes",
"Description": ""
}
{
"ID": "12",
"Type": "Rice & Chips",
"Description": ""
}
{
"ID": "13",
"Type": "Rice Box",
"Description": ""
}
{
"ID": "14",
"Type": "Breads",
"Description": ""
}
{
"ID": "15",
"Type": "Dips & Drinks",
"Description": ""
}
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,472
  • Joined: 08-August 08

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 06:24 AM

What you're doing is concatenating the json encoded elements of the array. You could do something like this:
$result = mysqli_fetch_all($stmt,MYSQLI_ASSOC);
$json = '';
foreach($result as $row) {
    $json .= json_encode($row, JSON_PRETTY_PRINT);
}

echo $json;


P.S. Use prepared statements! If your query changes to use input from the user it will be vulnerable to SQL injection attacks.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 09:57 AM

I don't know if this is just an exercise or what, but that output in post 5 won't decode, it's not valid JSON. It's just printing a bunch of JSON objects. The individual objects will decode, but not the entire output. I'm not sure if that matters, but I thought I'd point it out.

Also, after that code finishes, $json contains a single object, not all of them.
Was This Post Helpful? 1
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,472
  • Joined: 08-August 08

Re: Why does json_encode put my json object inside an array [] ?

Posted 22 September 2017 - 11:13 AM

True. I was wondering how it's being used too. I'd assumed the code shown is just a snippet to show the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1