PHP检查数组是否包含所有位置

时间:2016-02-29 08:56:08

标签: php arrays multidimensional-array

我有一个数组数组: 例如:

array(
     array([name]=>AAA
           [size]=>300x250
           [count]=>10
          )
     array([name]=>AAA
           [size]=>160x600
           [count]=>9
          )
     array([name]=>AAA
           [size]=>728x90
           [count]=>3
          )
     array([name]=>CC
           [size]=>300x250
           [count]=>66
          )
     array([name]=>CC
           [size]=>160x600
           [count]=>55
          )
     array([name]=>DD
           [size]=>728x90
           [count]=>100
          )          
)

我有这个数组:

array("300x250", "160x600", "728x90", "300x600", "300x300", "120x600", "100x72", "970x250", "750x200", "120x60", "200x600");

我需要检查每个'name'是否包含最后一个数组的所有大小,如果不存在我需要添加。 期望结果是“名称”AAA重复11次,“名称CC”'DD'相同,因此一个,具有最后一个数组的所有大小。

我尝试为每个人做2比较,但不能与'名字'AA和其他人区别开来 我也有场数。所以我需要保持初始'计数',每次我添加一个缺失位置时我需要使新位置的'count'值= 0

有人可以帮我检查一下吗? 我的实际代码

  $tmp22222=array();
        foreach ($result as &$v){
            foreach ($sizes as $s) {
                if($v['size']==$s) {
                    array_push($tmp22222, $v);
                break;
                }
                if(!in_array($tmp22222, $v['size'])){
                    $v['size'] = $s;
                    $v['count'] = 0;
                    array_push($tmp22222, $v);
                }

            }
        }


    var_dump($tmp22222);

EXCPECTED RESULT:

array(
     array([name]=>AAA
           [size]=>300x250
           [count]=>10
          )
     array([name]=>AAA
           [size]=>160x600
           [count]=>9
          )
     array([name]=>AAA
           [size]=>728x90
           [count]=>3
          )
     array([name]=>AAA
           [size]=>300x600
           [count]=>0
          )
     array([name]=>AAA
           [size]=>300x300
           [count]=>0
          )
     array([name]=>AAA
           [size]=>120x600
           [count]=>0
          )
     array([name]=>AAA
           [size]=>100x72
           [count]=>0
          )
     array([name]=>AAA
           [size]=>970x250
           [count]=>0
          )
     array([name]=>AAA
           [size]=>750x200
           [count]=>0
          )
     array([name]=>AAA
           [size]=>120x60
           [count]=>0
          )
     array([name]=>AAA
           [size]=>200x600
           [count]=>0
          )

     array([name]=>CC
           [size]=>300x250
           [count]=>66
          )
     array([name]=>CC
           [size]=>160x600
           [count]=>55
          )
     array([name]=>CC
           [size]=>728x90
           [count]=>100
          )
     array([name]=>CC
           [size]=>300x600
           [count]=>0
          )
     array([name]=>CC
           [size]=>300x300
           [count]=>0
          )
     array([name]=>CC
           [size]=>120x600
           [count]=>0
          )
     array([name]=>CC
           [size]=>100x72
           [count]=>0
          )
     array([name]=>CC
           [size]=>970x250
           [count]=>0
          )
     array([name]=>CC
           [size]=>750x200
           [count]=>0
          )
     array([name]=>CC
           [size]=>120x60
           [count]=>0
          )
     array([name]=>CC
           [size]=>200x600
           [count]=>0
          )

添加了'count'= 0的所有位置 如您所见,第一个数组的现有大小和计数继续使用相同的值,只需添加count = 0的新大小

3 个答案:

答案 0 :(得分:2)

尝试以下简单的解决方案:

    $array = array(
        array('name' => 'AAA',
            'size' => '300x250',
            'count' => '10',
        ),
        array('name' => 'AAA',
            'size' => '160x600',
            'count' => '9',
        ),
        array('name' => 'AAA',
            'size' => '728x90',
            'count' => '7',
        ),
        array('name' => 'CC',
            'size' => '300x250',
            'count' => '5',
        ),
        array('name' => 'CC',
            'size' => '160x600',
            'count' => '7',
        ),
        array('name' => 'DD',
            'size' => '728x90',
            'count' => '1',
        )
    );

    $size_array = array("300x250", "160x600", "728x90", "300x600", "300x300", "120x600", "100x72", "970x250", "750x200", "120x60", "200x600");

    //get unique name from original array
    $names = array_unique(array_column($array, 'name'));
    /**
 * for php < 5.5 change above line to
 *
   $names = array_map(function($element) {
     return $element['name'];
    }, $array);
    $names = array_unique($names);
 */
    foreach($names as $name){
        $size_exists_array = array_filter($array, function($val) use($name){
                if($val['name'] == $name){
                    return true;
                } else {
                    return false;
                }
        });

        //get already exists size
        $size = array_unique(array_column($size_exists_array, 'size'));
        /**
     * for php < 5.5 change above line to
     *
       $size = array_map(function($element) {
        return $element['size'];
      }, $size_exists_array);
      $size = array_unique($size);
     */
        //calculate difference of exist and available size
        $diff = array_diff($size_array, $size);

        //insert non exists size for name
        if($diff){
            foreach($diff as $dif){
                $array[] = array('name' => $name, 'size' => $dif, 'count' => '0',);
            }
        }
    }
    sort($array);

    print_r($array);

<强>输出:

Array
(
    [0] => Array
        (
            [name] => AAA
            [size] => 100x72
            [count] => 0
        )

    [1] => Array
        (
            [name] => AAA
            [size] => 120x60
            [count] => 0
        )

    [2] => Array
        (
            [name] => AAA
            [size] => 120x600
            [count] => 0
        )

    [3] => Array
        (
            [name] => AAA
            [size] => 160x600
            [count] => 9
        )

    [4] => Array
        (
            [name] => AAA
            [size] => 200x600
            [count] => 0
        )

    [5] => Array
        (
            [name] => AAA
            [size] => 300x250
            [count] => 10
        )

    [6] => Array
        (
            [name] => AAA
            [size] => 300x300
            [count] => 0
        )

    [7] => Array
        (
            [name] => AAA
            [size] => 300x600
            [count] => 0
        )

    [8] => Array
        (
            [name] => AAA
            [size] => 728x90
            [count] => 7
        )

    [9] => Array
        (
            [name] => AAA
            [size] => 750x200
            [count] => 0
        )

    [10] => Array
        (
            [name] => AAA
            [size] => 970x250
            [count] => 0
        )

    [11] => Array
        (
            [name] => CC
            [size] => 100x72
            [count] => 0
        )

    [12] => Array
        (
            [name] => CC
            [size] => 120x60
            [count] => 0
        )

    [13] => Array
        (
            [name] => CC
            [size] => 120x600
            [count] => 0
        )

    [14] => Array
        (
            [name] => CC
            [size] => 160x600
            [count] => 7
        )

    [15] => Array
        (
            [name] => CC
            [size] => 200x600
            [count] => 0
        )

    [16] => Array
        (
            [name] => CC
            [size] => 300x250
            [count] => 5
        )

    [17] => Array
        (
            [name] => CC
            [size] => 300x300
            [count] => 0
        )

    [18] => Array
        (
            [name] => CC
            [size] => 300x600
            [count] => 0
        )

    [19] => Array
        (
            [name] => CC
            [size] => 728x90
            [count] => 0
        )

    [20] => Array
        (
            [name] => CC
            [size] => 750x200
            [count] => 0
        )

    [21] => Array
        (
            [name] => CC
            [size] => 970x250
            [count] => 0
        )

    [22] => Array
        (
            [name] => DD
            [size] => 100x72
            [count] => 0
        )

    [23] => Array
        (
            [name] => DD
            [size] => 120x60
            [count] => 0
        )

    [24] => Array
        (
            [name] => DD
            [size] => 120x600
            [count] => 0
        )

    [25] => Array
        (
            [name] => DD
            [size] => 160x600
            [count] => 0
        )

    [26] => Array
        (
            [name] => DD
            [size] => 200x600
            [count] => 0
        )

    [27] => Array
        (
            [name] => DD
            [size] => 300x250
            [count] => 0
        )

    [28] => Array
        (
            [name] => DD
            [size] => 300x300
            [count] => 0
        )

    [29] => Array
        (
            [name] => DD
            [size] => 300x600
            [count] => 0
        )

    [30] => Array
        (
            [name] => DD
            [size] => 728x90
            [count] => 1
        )

    [31] => Array
        (
            [name] => DD
            [size] => 750x200
            [count] => 0
        )

    [32] => Array
        (
            [name] => DD
            [size] => 970x250
            [count] => 0
        )

)

答案 1 :(得分:2)

如果更改数组格式会变得更简单:

// reformat array into ['AAA' => ['300x250' => 10, ..]]
$grouped = array_reduce($result, function (array $grouped, array $item) {
    $grouped[$item['name']][$item['size']] = $item['count'];
    return $grouped;
}, array());

// create default size => 0 mapping
$defaultSizes = array_fill_keys($sizes, 0);

// add default size => 0 map to each name group, doesn't overwrite existing values
$grouped = array_map(function (array $group) use ($defaultSizes) {
    return $group + $defaultSizes;
}, $grouped);

// reformat into original structure
$result = array_reduce(array_keys($grouped), function (array $result, array $name) use ($grouped) {
    foreach ($grouped[$name] as $size => $count) {
        $result[] = compact('name', 'size', 'count');
    }
    return $result;
}, array());

答案 2 :(得分:0)

这不是一种有效的方法,但它有很多组合。

 $sizes = array("300x250", "160x600", "728x90", "300x600", "300x300", "120x600", "100x72", "970x250", "750x200", "120x60", "200x600");
$ex = array(
     array("name"=>"AAA",
           "size"=>"300x250",
           "count"=>10
          ),
     array("name"=>"AAA",
           "size"=>"160x600",
           "count"=>9
          ),
     array("name"=>"AAA",
           "size"=>"728x90",
           "count"=>3
          ),
     array("name"=>"CC",
           "size"=>"300x250",
           "count"=>66
          ),
     array("name"=>"CC",
           "size"=>"160x600",
           "count"=>55
          ),
     array("name"=>"DD",
           "size"=>"728x90",
           "count"=>100
          )          
);

$reorderedEx = array();

foreach($ex as $item){
  $reorderedEx[$item["name"]][$item["size"]] = $item["count"];
}
foreach($sizes as $size){
  foreach($reorderedEx as $name=>$r){
        if(empty($reorderedEx[$name][$size])){
            $reorderedEx[$name][$size] = "0";
        }
  }
}
$reconstruct = array();
foreach($reorderedEx as $name => $val){
    foreach($val as $size => $count){
        $reconstruct[] = array("name" => $name,"size"=>$size,"count"=>$count);
    } 

}

echo "<pre>",print_r($reconstruct,true);

结果:

Array
(
    [0] => Array
        (
            [name] => AAA
            [size] => 300x250
            [count] => 10
        )

    [1] => Array
        (
            [name] => AAA
            [size] => 160x600
            [count] => 9
        )

    [2] => Array
        (
            [name] => AAA
            [size] => 728x90
            [count] => 3
        )

    [3] => Array
        (
            [name] => AAA
            [size] => 300x600
            [count] => 0
        )

    [4] => Array
        (
            [name] => AAA
            [size] => 300x300
            [count] => 0
        )

    [5] => Array
        (
            [name] => AAA
            [size] => 120x600
            [count] => 0
        )

    [6] => Array
        (
            [name] => AAA
            [size] => 100x72
            [count] => 0
        )

    [7] => Array
        (
            [name] => AAA
            [size] => 970x250
            [count] => 0
        )

    [8] => Array
        (
            [name] => AAA
            [size] => 750x200
            [count] => 0
        )

    [9] => Array
        (
            [name] => AAA
            [size] => 120x60
            [count] => 0
        )

    [10] => Array
        (
            [name] => AAA
            [size] => 200x600
            [count] => 0
        )

    [11] => Array
        (
            [name] => CC
            [size] => 300x250
            [count] => 66
        )

    [12] => Array
        (
            [name] => CC
            [size] => 160x600
            [count] => 55
        )

    [13] => Array
        (
            [name] => CC
            [size] => 728x90
            [count] => 0
        )

    [14] => Array
        (
            [name] => CC
            [size] => 300x600
            [count] => 0
        )

    [15] => Array
        (
            [name] => CC
            [size] => 300x300
            [count] => 0
        )

    [16] => Array
        (
            [name] => CC
            [size] => 120x600
            [count] => 0
        )

    [17] => Array
        (
            [name] => CC
            [size] => 100x72
            [count] => 0
        )

    [18] => Array
        (
            [name] => CC
            [size] => 970x250
            [count] => 0
        )

    [19] => Array
        (
            [name] => CC
            [size] => 750x200
            [count] => 0
        )

    [20] => Array
        (
            [name] => CC
            [size] => 120x60
            [count] => 0
        )

    [21] => Array
        (
            [name] => CC
            [size] => 200x600
            [count] => 0
        )

    [22] => Array
        (
            [name] => DD
            [size] => 728x90
            [count] => 100
        )

    [23] => Array
        (
            [name] => DD
            [size] => 300x250
            [count] => 0
        )

    [24] => Array
        (
            [name] => DD
            [size] => 160x600
            [count] => 0
        )

    [25] => Array
        (
            [name] => DD
            [size] => 300x600
            [count] => 0
        )

    [26] => Array
        (
            [name] => DD
            [size] => 300x300
            [count] => 0
        )

    [27] => Array
        (
            [name] => DD
            [size] => 120x600
            [count] => 0
        )

    [28] => Array
        (
            [name] => DD
            [size] => 100x72
            [count] => 0
        )

    [29] => Array
        (
            [name] => DD
            [size] => 970x250
            [count] => 0
        )

    [30] => Array
        (
            [name] => DD
            [size] => 750x200
            [count] => 0
        )

    [31] => Array
        (
            [name] => DD
            [size] => 120x60
            [count] => 0
        )

    [32] => Array
        (
            [name] => DD
            [size] => 200x600
            [count] => 0
        )

)