doushengyou2617 2017-07-14 13:35
浏览 121
已采纳

当只有一个数组时,文本文件计数返回为2

Could someone please tell me when the decoded contents returns as count = 2 instead of 1?

I presume if the decode array was there then it should be a count of 1 because it's not got multiple array objects inside it.

Example:

// Count 1
Array ( [chat_id] => 414 [inserted] => 1500038898 )

// Count 2 
Array ( 
[0] => Array ([chat_id] => 414 [inserted] => 1500038898) 
[1] => Array ([chat_id] => 415 [inserted] => 1500038898) 
)

Text file contents:

{"chat_id":414,"inserted":1500038898}

Code:

// Get file contents
$fileContents = json_decode(file_get_contents($file), true);

if($fileContents !== NULL){
    // Push decoded contents to temp array
    $decoded_data = $fileContents;
    print_r(count($decoded_data));
}

Decoded:

Array ( [chat_id] => 414 [inserted] => 1500038898 )
  • 写回答

1条回答 默认 最新

  • dtrz99313 2017-07-14 13:45
    关注

    Dude, as I told you in my first comment, it is not an array that's there in the JSON. Okay, so JSON data is not upto your input's requirement. Change your JSON data to:

    [{"chat_id":414,"inserted":1500038898}]
    

    This should give you 1 instead of 2. The above is what you need for single entry. An example of two records, like how you wanted might be looking like:

    [{"chat_id":414,"inserted":1500038898}, {"chat_id":415,"inserted":1500038898}]
    

    I hope I was clear in explaining. You are missing []. It should be an array of objects [{}] not just an object {}.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测