0

I'm bringing json_encode data from the db.

foreach ($rslt as $val){
  $data[] = $val; 
}
echo json_encode($data);

the output is [{"column name":"dbData1"},{"column name":"dbData2"}]

Instead of the column name can I replace it to a custom name?

expected output:

[{"rec":"dbData1"},{"rec":"dbData2"}]
2
  • 1
    [ "newkey" => $val["oldkey"] ] Commented Jul 3, 2015 at 7:00
  • See this stackoverflow.com/a/5917539/1129313 Commented Jul 3, 2015 at 7:08

2 Answers 2

2

Something like this :

foreach ($rslt as $val){
    $data[] = array('rec'=>$val['column name']); 
}
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

Comments

0
<?php
$arr = array('rec' => $val["col_name"], 'rec' => $val["col_name2"]);

echo json_encode($arr);
?>

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.