Anshu Pratap Yadav Anshu Pratap Yadav - 7 months ago 34
PHP Question

Convert PHP Array to Json. Using Code-igniter

I Convert PHP Array To JSON Success But My target not full fill.

My out put is:

{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}


My Requirement :

[{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}]


Here my code:-

if ($AdminRecord) {

foreach($AdminRecord->result() as $results)
{
echo json_encode($results);
}
}

Answer

You are outputting every row, instead try this:

$resultSet = Array();
if($AdminRecord){
    foreach($AdminRecord->result() as $result) {
       $resultSet[] = $result;
    }
}
echo json_encode($resultSet);