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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)