Home > Backend Development > PHP Tutorial > json问题请教

json问题请教

WBOY
Release: 2016-06-23 14:14:38
Original
1035 people have browsed it

php使用json_encode可否实现以下格式的输出,如果可以,能否发个例子?非常感谢!

1条结果返回:{"result":[{"userid":001,"username":"李明"}]}

2条果结返回:{"result":[{"userid":001,"username":"张飞"},{"userid":002,"username":"张强"}]}


回复讨论(解决方案)

$data = array();$data['result'] = array();// 任意条$data['result'][] = array('userid' => '001', 'username': '李明');
Copy after login

JSON中,[]表示是一个数组,它的特点是没有键名,通常用来代表一个包含多组对象的值。{}表示是一个对象,它的特点是键值对应。

$a = array(  'result' => array(    array( 'userid' => '001', 'username' => '李明'),  ),);array_walk_recursive($a, 'encode');echo urldecode(json_encode($a));$a = array(  'result' => array(    array( 'userid' => '001', 'username' => '张飞'),    array( 'userid' => '001', 'username' => '张飞'),  ),);array_walk_recursive($a, 'encode');echo urldecode(json_encode($a));function encode(&$item, $key) {  $item = urlencode($item);}
Copy after login
{"result":[{"userid":"001","username":"李明"}]}

{"result":[{"userid":"001","username":"张飞"},{"userid":"001","username":"张飞"}]}

观看,有些不太懂,学习。

感谢xuzuning与dream1206!

请问xuzuning,
$a = array(
  'result' => array(
    array( 'userid' => '001', 'username' => '张飞'),
    array( 'userid' => '001', 'username' => '张飞'),
  ),
);

以上数组中,"array( 'userid' => '001', 'username' => '张飞'),"能否是一个变量,可以动态的增加呢,比如:
$a = array(
  'result' => array(
    array( 'userid' => '001', 'username' => '张飞1'),
    array( 'userid' => '001', 'username' => '张飞2'),
    array( 'userid' => '001', 'username' => '张飞3'),
  ),
);

谢谢!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template