【问题标题】:Check if every sub-array of a multidimensional array contains an element or not检查多维数组的每个子数组是否包含一个元素
【发布时间】:2017-12-27 22:03:40
【问题描述】:

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

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

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

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

    [2] => Array
        (
        )

    [3] => Array
        (
        )

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

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

)

但由于它是电子商务网站的类别层次结构,您可以放心地想象,网站可以有任何深度的类别、子类别、子子类别等。产品类别的层次结构未知,因此类别 ids 数组的深度未知。

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

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

【问题讨论】:

  • 您是否需要知道该项目在哪里或仅在它在那里时才知道?
  • 只需要知道每个子数组或子子数组是否包含该项目(在我的情况下为category-id)。如果可以找到项目的位置,那就太好了,但是“是否存在”也很有帮助。
  • 如果存在,可以通过展平数组并使用 in_array 来完成。这是第一步。我想如果那里是真的,你需要一个递归函数来找到位置。 stackoverflow.com/questions/4128323/…stackoverflow.com/questions/526556/…

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


【解决方案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;
}

【讨论】:

    【解决方案2】:

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

    你可以使用这样的东西

    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);

    【讨论】:

      【解决方案3】:

      您可以使用 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
                      )
      
              )
      
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-30
        • 2020-11-21
        • 2017-03-03
        • 1970-01-01
        • 1970-01-01
        • 2019-05-05
        相关资源
        最近更新 更多