从数组中的所有索引获取公共值

时间:2017-02-16 06:27:37

标签: php arrays

我有一个像这样的数组,

Array
(
  [0] => Array
    (
        [0] => Array
            (
                [productId] => 145
                [variantId] => 179
            )
        [1] => Array
            (
                [productId] => 145
                [variantId] => 180
            )
        [2] => Array
            (
                [productId] => 147
                [variantId] => 181
            )
        [3] => Array
            (
                [productId] => 147
                [variantId] => 182
            )
    )
[1] => Array
    (
        [0] => Array
            (
                [productId] => 147
                [variantId] => 181
            )
        [1] => Array
            (
                [productId] => 147
                [variantId] => 184
            )
    )
[2] => Array
    (
        [0] => Array
            (
                [productId] => 145
                [variantId] => 178
            )
        [1] => Array
            (
                [productId] => 145
                [variantId] => 180
            )
        [2] => Array
            (
                [productId] => 147
                [variantId] => 181
            )
        [3] => Array
            (
                [productId] => 147
                [variantId] => 185
            )
        [4] => Array
            (
                [productId] => 147
                [variantId] => 186
            )
    )
)

对于此输入,我们可以看到[productId] => 147,[variantId] => 181是数组的所有索引。

我正在使用foreach来检查这一点。但我的代码适用于2索引,但不超过2索引。

foreach我正在使用0th索引并与其他索引进行比较,但无效。

这是我到目前为止使用的代码。

  $first = $name1[0];
    $a = array();
    unset($name1[0]);

    foreach ($name1 as $row) {// foreach main array
        foreach ($first as $row1) {// foreach first index of array array
            foreach ($row as $kkk => $r1) {//foreach index of main array
                // if first array's index's product is is equal to main array index's product id, push values to one array
                if ($row1['productId'] == $r1['productId']) {
                    if (isset($row1['variantId']) && isset($r1['variantId'])) {
                        if ($row1['variantId'] == $r1['variantId']) {
                            $a[] = $r1;
                        }
                    } else {
                         $a[] = $r1;
                    }
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

第一种方法

使用它,PHP函数比尝试构建自己的算法更快。

$result = array_intersect($array1, $array2);

第二种方法

$array1 = array("5","26","38","42");

$array2 = array("15","36","38","42");

$final_array = array();

foreach($array1 as $key=>$val){
    if(in_array($val,$array2)){
        $final_array[] = $val;
    }
}

print_r($final_array);

结果:数组([0] => 38 [1] => 42)

for 2d array

function multi_intersect($arr) {
   $return = array();
   foreach ($arr as $a) {
       foreach ($arr as $b) {
           if ($a === $b) continue;
           $return = array_merge($return, array_intersect($a, $b));
       }
   }
   return array_unique($return);
}

你应该得到

Array
(
    [0] => 58
)

所以最后你用它来得到结果

$intersect = call_user_func_array('array_intersect', $arr);

答案 1 :(得分:0)

您可以在foreach循环

中使用foreach
$my_array = array
(
  '0' => array
    (
        '0' => array
            (
                'productId' => 145,
                'variantId' => 179
            ),
        '1' => array
            (
                'productId' => 145,
                'variantId' => 180
            ),
        '2' => array
            (
                'productId' => 147,
                'variantId' => 181
            ),
        '3' => array
            (
                'productId' => 147,
                'variantId' => 182
            )
    ),
'1' => array
    (
        '0' => array
            (
                'productId' => 147,
                'variantId' => 181
            ),
            '1' => array
            (
                'productId' => 147,
                'variantId' => 184
            )
    ),
'2' => array
    (
        '0' => array
            (
                'productId' => 145,
                'variantId' => 178
            ),
        '1' => array
            (
                'productId' => 145,
                'variantId' => 180
            ),
        '2' => array
            (
                'productId' => 147,
                'variantId' => 181
            ),
        '3' => array
            (
                'productId' => 147,
                'variantId' => 185
            ),
        '4' => array
            (
                'productId' => 147,
                'variantId' => 186
            )
    )
);

$final_array = array();

foreach($my_array as $nested_array){
    foreach($nested_array as $val ){
        $final_array[] = $val;
    }

}

echo "<pre>"; print_r($final_array);

答案 2 :(得分:0)

这是我想要的答案,

function serialize_array_values($arr){
    foreach($arr as $key=>$val){
        sort($val);
        $arr[$key]=serialize($val);
   }

    return $arr;
}




$result = array_map("unserialize", array_intersect(
serialize_array_values($name1[0]),
serialize_array_values($name1[1]),
serialize_array_values($name1[2])
));

https://eval.in/738165,查看工作链接

参考:array_intersect throws errors when arrays have sub-arrays

最后这是我的工作代码

foreach ($name1 as $kkk => $row) {
    $list[] = $this->serializeArrayValues($row);
}

$intersect = call_user_func_array('array_intersect', $list);
$a = array_map("unserialize", $intersect);

function serializeArrayValues($arr) {
    foreach ($arr as $key => $val) {

        $arr[$key] = serialize($val);
    }

    return $arr;
}