简体   繁体   English

我如何用php计算一个多维数组中具有相同名称的数组?

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

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_shipping'][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语句。

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. 在04/03/2010中有1个选择。
There are 1 of the 04/12/2010 selection. 在04/12/2010中有1个选择。
There are 1 of the 04/12/2010 selection. 在04/12/2010中有1个选择。

I would like it to output this: 我想要它输出:

There are 1 of the 04/03/2010 selection. 在04/03/2010中有1个选择。
There are 2 of the 04/12/2010 selection. 在04/12/2010中有2个选择。

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. 而是在$date_value上调用count() ,因为您要计算映射到该键的数组值中的项数,而不是键本身的大小。

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 您正在计算错误的变量,它必须是$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 您可以创建一个RecursiveIteratorIteratorRecursiveArrayIterator

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

and then iterate over it easily with foreach 然后使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM