dongxiandi8313 2015-09-29 20:02 采纳率: 100%
浏览 62
已采纳

PHP sizeof()返回1错误

This code worked last week, and I don't remember changing this code recently, but for some reason it stopped working today.

I've narrowed down the issue, but I'm confused about it. Basically, $fields = sizeof($dataArray[$r]); yields $fields = 1, but as you can see in the image below, ECHO json_encode($dataArray); demonstrates that $fields = 9 should be observed.

$dataArray = $_POST['array'];

$validRows = array();
$invalidRows = array();

echo sizeof($dataArray); // correct output

for($r = 0; $r < sizeof($dataArray); $r++){

  $fields = sizeof($dataArray[$r]); // $fields is not what I expect

  ECHO json_encode($dataArray); // see image below (sent to javaScript alert)
  ECHO sizeof($fields); // returns 1

  ...
}

ECHO json_encode($dataArray); Seen in a javaScript alert

enter image description here

What's my issue here?

  • 写回答

1条回答 默认 最新

  • dongzhong8691 2015-09-29 20:12
    关注

    Based on PHP Documentation, sizeOf (alias of Count) returns 1 if the array is not countable .

    http://php.net/manual/en/function.count.php

    Returns the number of elements in array_or_countable. If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.

    But appart from that, on your second ECHO, you are displaying the sizeOff ot the result of a sizeoff , that would be a single number pseudo: a = count("a","b","c") a should be 3 ECHO count(a) ECHO Display 1, as (a) is a single element

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

报告相同问题?

悬赏问题

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