Maison > Article > développement back-end > json问题请教
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': '李明');
$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);}{"result":[{"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'),
),
);
谢谢!