Add Additional Objects to JSON Encoded Array

后端 未结 4 933
名媛妹妹
名媛妹妹 2021-02-06 04:21

I am currently using a JSON encoded array to display the users in my database for an auto-suggest feature.

It looks something like this:

$sth = mysql_que         


        
4条回答
  •  情书的邮戳
    2021-02-06 04:28

    Just keep pushing to the $data array.

    $json = array();
    
        while($row = mysql_fetch_assoc($sth)) {
            $json['name'] = $row['name'];
            $json['id'] = $row['id'];
            $data[] = $json;
        }
    
    $custom = array('name'=>'foo', 'id' => 'bar');
    $data[] = $custom;
    

    Then at the very end, do your json_encode. Assuming you're not referring to merging it in the JS itself with multiple ajax calls.

    And if you have separate scripts, combine them in one php page.

提交回复
热议问题