1

I am creating a json file from my array:

$file = json_encode($array);

The json file will look like this:

     [
      {
          "name": "file1.html",
          "date": "2019-01-29T20:33:57.000163Z",
          "size": "348"
      }
      {
          "name": "file2.xml",
          "date": "2019-01-29T20:33:57.000167Z",
          "size": "401"
      }
      {
          "name": "file3.html",
          "date": "2019-01-29T20:33:57.000171Z",
          "size": "1314"
      }
   ]

But I need to create a json file with some little bit different format. The output I need is:

{ 
 "draw": 1,
 "recordsTotal": 5000,
 "recordsFiltered": 5000,
 "data": [
      {
          "name": "file1.html",
          "date": "2019-01-29T20:33:57.000163Z",
          "size": "348"
      }
      {
          "name": "file2.xml",
          "date": "2019-01-29T20:33:57.000167Z",
          "size": "401"
      }
      {
          "name": "file3.html",
          "date": "2019-01-29T20:33:57.000171Z",
          "size": "1314"
      }
   ]
}

Is this possible with json_encode?

2
  • Where do the other values come from? Commented Feb 11, 2020 at 12:46
  • @Nick I need to add them manually Commented Feb 11, 2020 at 12:47

1 Answer 1

4

Create a new array with rest of the info and assign current array data into it as well.

$newArray = array(
    'draw'=> 1,
    'recordsTotal'=> 5000,
    'recordsFiltered'=> 5000,
    'data'=>$array
);

$file = json_encode($newArray);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.