return multiple json results

萝らか妹 提交于 2021-02-05 05:55:13

问题


I have to output $var1. To do that, I return results by using

sendJson($var1);

And this is working fine for me. Now I want to pass two variables $var1 and $var2 using sendJson();. Is it possible?


回答1:


Put them all inside an array:

sendJson(array($var1, $var2));

Or name them if you don't want to do it index-based:

sendJson(array('var1' => $var1, 'var2' => $var2))



回答2:


name your array keys:

echo json_encode( array (
    'messages' => array(
        'peter' => 'Hello',
        'john'  => 'Hi to you too'
    ),
    'users'     => array (
        'peter', 'john'
    )
));

and then access them like this:

resp.messages.peter
resp.messages.john

or

resp.users[0], resp.users[1].... etc.



来源:https://stackoverflow.com/questions/13910024/return-multiple-json-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!