代码之家  ›  专栏  ›  技术社区  ›  Bijan Zand

把数组放到foreach之后就不能算了?

  •  3
  • Bijan Zand  · 技术社区  · 5 年前

    我应该找到今天的时间戳,如果计数超过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
    )
    

    这一步我要数数 timestamp 今天要找到3个时间戳,但是 count 返回 1 不工作!

    更新完整代码:

    $query->condition('node_revision.nid', $node->nid);
    
    $result = $query->execute();
    
        while( $record = $result->fetchAssoc() ) { 
            $items[] = $record;
        }
    
    
    foreach ($items as $Newarrays) {
    
    
        $timestamp=$Newarrays["timestamp"];
        $accessdate=format_date($timestamp, 'custom', 'd-m-Y' );
        $currentdate=date("d-m-Y");
    
    
    
        if ($accessdate === $currentdate) {
             if (count( $accessdate > 3 )) {
                print_r ($accessdate);
            }
    
        }
    
    }
    

    怎么做到的? 谢谢你的帮助…

    1 回复  |  直到 5 年前
        1
  •  1
  •   Dilip Hirapara    5 年前

    像这样试试。在这个输出中,我得到一个 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