Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数组上匹配子数组并对其计数_Php_Arrays - Fatal编程技术网

Php 在数组上匹配子数组并对其计数

Php 在数组上匹配子数组并对其计数,php,arrays,Php,Arrays,我有一个数组 Array ( [1] => Array ( [0] => 1 [1] => 3 ) [2] => Array ( [0] => 1 [1] => 2 ) [3] => Array ( [0] => 1

我有一个数组

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

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

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

)
我需要找到公共子阵列

在上面的示例中,数组1和3具有公共子数组

(
   [0] => 1
   [1] => 3
)
所以最后一个数组必须是

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

    [2] => Array
        (
            [0] => 1
            [1] => 2
        )
)
但我需要计算一些常用值


任何建议。

只需将数组中的每个元素与其他元素进行比较,假设它们是线性数组,但使用来比较每个元素。如果它们不同,将元素或数组索引复制到另一个数组中

只需将数组中的每个元素与其他元素进行比较,假设它们是线性数组,但使用来比较每个元素。如果它们不同,将元素或数组索引复制到另一个数组中

我不会将其用于生产代码,但这里有一种快速且有点聪明的方法:

$arrays = array(array(1,3), array(1,2), array(1,3)); // Your example data

$serialized = array_map('serialize', $arrays);
$counts = array_count_values($serialized);
foreach ($counts as $data => $count) {
  echo "$count: " . print_r(unserialize($data), true);
}

我不会将其用于生产代码,但这里有一种快速且有点聪明的方法:

$arrays = array(array(1,3), array(1,2), array(1,3)); // Your example data

$serialized = array_map('serialize', $arrays);
$counts = array_count_values($serialized);
foreach ($counts as $data => $count) {
  echo "$count: " . print_r(unserialize($data), true);
}
is
array(1,3)==array(3,1)
?is
array(1,3)==array(3,1)