【问题标题】:After set array into foreach cant count that?将数组设置为 foreach 后无法计算?
【发布时间】:2019-10-12 12:06:16
【问题描述】:

我应该找到今天的时间戳,如果计数超过 3,则返回 false,如果不返回 true

查询:

while( $record = $result->fetchAssoc() ) { 
    $items[] = $record;
}

输出:

Array
(
    [0] => Array
        (
            [timestamp] => 1570877769
        )

    [1] => Array
        (
            [timestamp] => 1570877783
        )

    [2] => Array
        (
            [timestamp] => 1510877794
        )

)

Foreach:

    foreach ($items as $Newarrays) {

    }

使用 foreach 循环后查看:

Array
(
    [timestamp] => 1570877769
)
Array
(
    [timestamp] => 1570877783
)
Array
(
    [timestamp] => 1570877794
)

这一步我想计数 timestamps 今天找到 3 个时间戳,但 count 返回 1 并且不起作用!

更新完整代码:

$query->condition('node_revision.nid', $node->nid);

怎么做? 谢谢你的帮助...

【问题讨论】:

  • 你在哪里使用了count函数,你能显示它是显示1作为回报吗?
  • if ($accessdate === $currentdate) { if (count($timestamp) >3) { } }
  • 是的,请添加您的整个代码,以便我们可以假设您尝试了什么以及在哪里出错。编辑问题并输入整个代码。
  • count( $items) > 3
  • 我应该找到今天的时间戳$items 有所有的时间戳!

标签: php arrays foreach


【解决方案1】:

试试这样。在这个输出中,我得到一个 count 作为 2

   $items = array(array('timestamp'=>1570877769),array('timestamp'=>1570877783),array('timestamp'=>1510877794));


    $currentdate=date("d-m-Y");

    $count = 0;
    foreach ($items as $newarrays) {
        if($currentdate == date("d-m-Y",$newarrays['timestamp'])){
            $count++;
        }
    }
    echo $count;
    exit;

https://paiza.io/projects/WpyO1P-9Oaj3G_gYq4VeQA

【讨论】:

  • 你有没有替换 exit; 每次使用该脚本停止!
  • 是的,您可以删除它。我只是想向您展示如何计算数组中的值不要专注于退出;你可以删除它。
猜你喜欢
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多