0

Any help would be appreciated!

I've been going at this for a little bit now and can't seem to get this to work. What am I doing wrong?

I'm trying to get the output of a JSON structure below using php. Take a look at the differences with the outputs below!

$mysqli = new mysqli('localhost','root','1234','root');
$records = array();
if ($result = $mysqli->query("SELECT * FROM users")) {

    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $records[] = $row;
    }
    $json = json_encode($records, JSON_PRETTY_PRINT);

}

   file_put_contents('records.js', $json, FILE_APPEND);

What it's outputting now:

[

  {
    "id": "2",
    "volume": "volume1",
    "name": "a",
    "content": "<img src="image_2.jpg">",
    "cssanimate": "bounce"
  },
  {

      "id": "2",
      "volume": "volume1",
      "name": "a",
      "content": "<img src="image_2.jpg">",
      "cssanimate": "bounce"
   },
   {

      "id": "2",
      "volume": "volume1",
      "name": "a",
      "content": "<img src="image_2.jpg">",
      "cssanimate": "bounce"
    } 

]

What I want the output to be:

var data = [{

  {
  tags: [{
    "id": "2",
    "volume": "volume1",
    "name": "a",
    "content": "<img src="image_2.jpg">",
    "cssanimate": "bounce"
      }]
  },
  {
    tags: [{
      "id": "2",
      "volume": "volume1",
      "name": "a",
      "content": "<img src="image_2.jpg">",
      "cssanimate": "bounce"
      }]
   },
   {
    tags: [{
      "id": "2",
      "volume": "volume1",
      "name": "a",
      "content": "<img src="image_2.jpg">",
      "cssanimate": "bounce"
      }]
    } 

}]
5
  • What is $data? Commented Sep 29, 2017 at 16:04
  • If you want tags key - you should add it by yourself. No one will do it for you. Commented Sep 29, 2017 at 16:05
  • @u_mulder sorry! That was an error :) Commented Sep 29, 2017 at 16:06
  • @u_mulder haha thanks for the comment but I can't figure it out... that's why I'm here asking the pros. I'm not asking for the answer... maybe some guidance? Commented Sep 29, 2017 at 16:07
  • And btw - do you understand difference between [ and { in json? Are you sure you need array of objects of objects? I bet this is invalid structure. Commented Sep 29, 2017 at 16:07

1 Answer 1

2

You have an extra level of { that's invalid in the desired output, so I'll ignore that. To create an object with the tags key, you need to create a PHP associative array using that key. And then to make the tags an array of objects, you need to wrap the row in another array.

while($row = $result->fetch_array(MYSQL_ASSOC)) {
    $records[] = array('tags' => array($row));
}

I'm not sure why you need each object in a single-element array nested inside another object and array, but that's a separate issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! @Barmar I see now how this is done and learned from it. Thanks buddy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.