PHP count items in a multi-dimensional array

前端 未结 9 1054
走了就别回头了
走了就别回头了 2021-01-11 19:54

As you can see from the following array, there are three elements that appear on Nov 18, and another two elements that appear on Nov 22. Can someone tell me how I can retri

相关标签:
9条回答
  • 2021-01-11 20:16

    You can use:

    count($array, COUNT_RECURSIVE);
    

    Count number of leaves in nested array tree

    0 讨论(0)
  • 2021-01-11 20:17

    Does this work for what you need?

    $dates = array(array(array("2011-11-18 00:00:00" => C), array("2011-11-18 00:00:00" => I),array
    ("2011-11-18 00:00:00" => S)),
    array(array("2011-11-22 00:00:00" => C), array("2011-11-22 00:00:00" => S)));
    
    $date_count = array();  // create an empty array
    
    foreach($dates as $date) {  // go thought the first level
        foreach($date as $d) {  // go through the second level
            $key = array_keys($d);  // get our date
            // here we increment the value at this date
            // php will see it as 0 if it has not yet been initialized
            $date_count[$key[0]]++;
        }
    }
        // show what we have
    print_r($date_count);
    

    Prints:

    Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 2 )
    

    Note: this assumes that you will always be getting data as you structured your array and that each date will be formatted the same. If you can't assume each date will be formatted, this would be a simple conversion using the date() function. If you can't assume that you will get data structured exactly like this, the best way to tackle that would probably be through a recursive function.

    0 讨论(0)
  • 2021-01-11 20:18

    You can use array_walk_recursive() to get access to all of the leaf nodes in an array structure.

    Something akin to this should work for you:

    <?php
    
    $data = array(
     array(
      array('2011-11-18 00:00:00' => 'C'),
      array('2011-11-18 00:00:00' => 'I'),
      array('2011-11-18 00:00:00' => 'S')),
     array(
      array('2011-11-22 00:00:00' => 'C'),
      array('2011-11-22 00:00:00' => 'S')));
    
    function countleafkeys($value, $key, $userData)
    {
      echo "$key\n";
      if(!isset($userData[$key])) {
        $userData[$key] = 1;
      } else {
        $userData[$key]++;
      }
    }
    
    $result = array();
    array_walk_recursive($data, 'countleafkeys', &$result);
    
    print_r($result);
    

    Outputs:

    2011-11-18 00:00:00
    2011-11-18 00:00:00
    2011-11-18 00:00:00
    2011-11-22 00:00:00
    2011-11-22 00:00:00
    Array
    (
        [2011-11-18 00:00:00] => 3
        [2011-11-22 00:00:00] => 2
    )
    
    0 讨论(0)
  • 2021-01-11 20:19
    <?php
    $count0=count($array[0], COUNT_RECURSIVE)-count($array[0]);
    $count1=count($array[1], COUNT_RECURSIVE)-count($array[1]);
    
    0 讨论(0)
  • 2021-01-11 20:33

    For your specific $array structure I think the most lean way is using foreach and then getting the date value and the count() out of each value:

    $dateCounts = array();
    foreach($array as $date)
    {
        $dateCounts[key($date[0])] = count($date);
    }
    var_dump($dateCounts);
    

    With your $array this gives:

    array(2) {
      ["2011-11-18 00:00:00"]=> int(3)
      ["2011-11-22 00:00:00"]=> int(2)
    }
    

    If you're looking for a more general way, you can make use of RecursiveArrayIterator and RecursiveIteratorIterator to traverse over all leaf key/value elements and then just count the keys:

    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
    $keyCounts = array();
    foreach ($it as $key => $value)
    {
        isset($keyCounts[$key]) ? $keyCounts[$key]++ : $keyCounts[$key] = 1; 
    } 
    var_dump($keyCounts);
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-11 20:34

    Here is my recursive variant:

    $arr = array(
        '0' => array(
            '0' => array('2011-11-18 00:00:00' => 'C'),
            '1' => array('2011-11-18 00:00:00' => 'I'),
            '2' => array('2011-11-18 00:00:00' => 'S')
        ),
        '1' => array(
            '0' => array('2011-11-22 00:00:00' => 'C'),
            '1' => array('2011-11-22 00:00:00' => 'S')
        ),
        '2' => array(
            '0' => array(
                '0' => array('2011-11-22 00:00:00' => 'D')
            )
        )
    );
    
    function count_values($array, &$result = array(), $counter = 0)
    {
        foreach ($array as $key => $data)
        {
            if (is_array($data))
            {
                count_values($data, $result, $counter);
            }
            else
            {
                array_key_exists($key, $result) ? $result[$key]++ : $result[$key] = 1;
            }
        }
    
        return $result;
    }
    
    print_r(count_values($arr));
    

    This will return:

    Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 3 )
    
    0 讨论(0)
提交回复
热议问题