1

i have this array

    array:82 [
  1000 => array:10 [
    "id" => 1000
    "name" => "name"
    "name2" => "name2"
    "name3" => "name3"
],
....
]

how to convert array to this json format ?

{
  "data": [
    {
      "id": "1000",
      "name1": "name",
      "name2": "name2",
      "name3": "name3"
    },
    ...
  ]
}

i try this but not work

$tableData = array();
$tableData['data'] = array_map(function($session) {
    return [$session['id'],$session['name'],$session['name2']];
}, $response);
 return response()->json($tableData);

anybody have idea?

i need this for datatable respons

1
  • What is $response? I noticed you tagged the question with Laravel, is it an Eloquent collection? Commented Apr 6, 2018 at 19:31

2 Answers 2

1

Do like below using json_encode()

$tableData = array();

foreach($session as $sess){
  $tableData['data'][] = ["id"=>$sess['id'],"name"=>$sess['name'],"name2"=>$sess['name2'],"name3"=>$sess['name3']];
}
return json_encode($tableData);
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the json_encode() function http://php.net/manual/en/function.json-encode.php

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.