检查多维数组的每个子数组是否包含元素

时间:2017-07-21 18:41:05

标签: php magento multidimensional-array element sub-array

我有一个多维数组,其深度不确定或让我们说取决于多少类别&子集类别(和子子类别)在magento网站上,因为我调用函数$product->getCategoryIds()

现在让我们假设我将所有产品(在购物车/结帐时添加)类别树层次结构作为多维数组,如下所示:

Array
(
    [0] => Array
        (
            [0] => 40
        )

    [1] => Array
        (
            [0] => 40
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
            [0] => 16
        )

    [5] => Array
        (
            [0] => 16
        )

)

但是因为它是ecomerce网站的类别heirarchy,你可以放心地想象,网站可以有任何深度的类别,子类别,子子类别等等。未知的产品类别层次结构,因此未知类别ID数组的深度。

现在我的困惑和疑问,如何通过使用各种php数组函数的组合来检查每个子数组和/或子子数组是否包含特定类别Id(例如40或16)并使用尽可能少的循环?

所有我能想到的是嵌套for/foreach循环,这会产生性能开销,所以我正在寻找更好的替代方案。

3 个答案:

答案 0 :(得分:1)

听起来你正在寻找这样的东西:

$ids = $product->getCategoryIds();
$non_empty_ids = array();
foreach ($ids as $key => $value) {
    if (!count($value)) {
        // The value has no offsets, lets skip this one
        continue;
    }
    $non_empty_ids[] = $value;
}

答案 1 :(得分:1)

当你不确定输入(数组的结构)时,我更喜欢使用递归

你可以使用这样的东西

function searchCategoriesForValue(array $array, array $needles)
{
    $matchsCount = 0;
    foreach ($array as $element){
        if(is_array($element)){
            $matchsCount += searchCategoriesForValue($element, $needles);
        }else{
            if(in_array($element, $needles)){
                $matchsCount ++;
            }
        }
    }
    return $matchsCount;
}

使用示例

<?php
// nested array that has the number 16 six times.
$array = [  [0,1,2,3,[16,40,[0,1]]],
            [0,1,2,3,[16,40,[0,1]]],
            [0,1,2,3,[16,40,[0,1]]],
            [0,1,2,3,[16,40,[0,1]]],
            [0,1,2,3,[16,40,[0,1]]],
            [],
            [0,1,2,3,[16,40,[0,1]]]];
$count = searchCategoriesForValue($array,[16]);
// 16 has been found 6 times
var_dump($count);
$count = searchCategoriesForValue($array,[16,40]);
// 16 and 40 have been found 12 times

exit;

此输出

  

INT(6)

现场演示(https://eval.in/835973);

答案 2 :(得分:0)

您可以使用array_filter()从数组中删除空类别

<?php

$myarray =  array('0' => 
                array('0' => '40'),
                '1' => array('0' => '40'),
                '2' => array(),
                '3' => array(),
                '4' => array('0' => '16'),
                '5' => array('0' => '16'),
                '6' => array('0' => 
                    array('0' => '40'),
                    '1' => array('0' => '40'),
                    '2' => array(),
                    '3' => array(),
                    '4' => array('0' => '16'),
                    '5' => array('0' => '16')
                )
            );

$b = array_map("array_filter", $myarray);
echo "<pre>";
print_r(array_filter($b));
echo "</pre>";
?>
Result:
Array
(
    [0] => Array
        (
            [0] => 40
        )

    [1] => Array
        (
            [0] => 40
        )

    [4] => Array
        (
            [0] => 16
        )

    [5] => Array
        (
            [0] => 16
        )

    [6] => Array
        (
            [0] => Array
                (
                    [0] => 40
                )

            [1] => Array
                (
                    [0] => 40
                )

            [4] => Array
                (
                    [0] => 16
                )

            [5] => Array
                (
                    [0] => 16
                )

        )

)