开发者

How do I count how many arrays have the same name within a multidimensional array with php?

开发者 https://www.devze.com 2022-12-25 17:39 出处:网络
I have a multidimensional array, and I would have multiple arrays within it. Some of those arrays contain multiple arrays within them as well, and I would like to count how many arrays are within the

I have a multidimensional array, and I would have multiple arrays within it. Some of those arrays contain multiple arrays within them as well, and I would like to count how many arrays are within the second array(the date).

This is an example of the structure of the multidimensional array:

$_SESSION['final_shi开发者_StackOverflow中文版pping'][04/03/2010][book]
$_SESSION['final_shipping'][04/12/2010][magazine]
$_SESSION['final_shipping'][04/12/2010][cd]

This is the foreach statement I am currently using to count how many of the second array(the one with the dates) exists.

foreach($_SESSION['final_shipping'] as $date_key => $date_value) { 
    foreach ($date_value as $product_key => $product_value) { 
        echo 'There are ' . count($date_key) . ' of the ' . $date_key . ' selection.<br/>';
    }
}

It is currently outputting this:

There are 1 of the 04/03/2010 selection.

There are 1 of the 04/12/2010 selection.

There are 1 of the 04/12/2010 selection.

I would like it to output this:

There are 1 of the 04/03/2010 selection.

There are 2 of the 04/12/2010 selection.


Call count() on $date_value instead, since you want to count the number of items in the array value mapped to that key, not the size of the key itself.

foreach($_SESSION['final_shipping'] as $date_key => $date_value) {
    echo 'There are ' . count($date_value) . ' of the ' . $date_key . ' selection.<br/>';
}


You are counting the wrong varibale it needs to be $date_value

foreach($_SESSION['final_shipping'] as $date_key => $date_value) { 
    echo 'There are ' . count($date_value) . ' of the ' . $date_key . ' selection.<br/>';
}


With a multidimensional array like this:

$data = array(
    'item1' => array(
        'item1-1' => array( 'foo' => 1,'bar' => 1 ),
        'item1-2' => array( 'bar' => 1 ),
    ),
    'item2' => array( 
        'item2-1' => array('foo' => 1 )
    )
);

You can create a RecursiveIteratorIterator and RecursiveArrayIterator

$it = new RecursiveIteratorIterator(
        new RecursiveArrayIterator($data),
        RecursiveIteratorIterator::SELF_FIRST);

and then iterate over it easily with foreach

foreach($it as $key => $val) {
    if(is_array($val)) {
        printf('There is %d of %s%s', count($val), $key, PHP_EOL);
    }
}

to receive

There is 2 of item1
There is 2 of item1-1
There is 1 of item1-2
There is 1 of item2
There is 1 of item2-1

You can limit this to only return the arrays of a certain depth with

foreach($it as $key => $val) {
    if(is_array($val) && $it->getDepth() === 1) {
        printf('There is %d of %s%s', count($val), $key, PHP_EOL);
    }
}

to receive

There is 2 of item1-1
There is 1 of item1-2
There is 1 of item2-1
0

精彩评论

暂无评论...
验证码 换一张
取 消