简体   繁体   English

如何使用PHP对用户数组进行JSON编码?

[英]How to JSON encode a user array using PHP?

Im working with a JSON file that checks, if a user is typing. 我正在处理一个JSON文件,该文件检查用户是否正在键入。

Is there any reason why this would not work? 有什么理由为什么这行不通?

  // Array of WP_User objects. foreach ( $user_query as $user ) { $result['whotyping'] = $user_info->whotyping; $result['typingto'] = $user_info->typingto; $result['typing'] = $user_info->typing; } echo json_encode($result); 

I thought this would work, but it returns nothing by an error. 我以为这会工作,但是由于错误它什么也不会返回。

How do I solve this problem? 我该如何解决这个问题?

  // Array of WP_User objects. foreach ( $user_query as $user ) { $result['whotyping'] = $user_info->whotyping; $result['typingto'] = $user_info->typingto; $result['typing'] = $user_info->typing; echo json_encode($result); } 

You should make an array of results. 您应该进行一系列结果。

  // Array of WP_User objects. $results = array(); foreach ( $user_query as $user ) { $result = array(); $result['whotyping'] = $user_info->whotyping; $result['typingto'] = $user_info->typingto; $result['typing'] = $user_info->typing; $results[] = $result; } echo json_encode($results); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM