简体   繁体   中英

php array of arrays remove array containing matching collection of values

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

  [1] => Array
    (
        [0] => 4937
        [1] => 4941
    )

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

  [3] => Array
    (
        [0] => 4937
        [1] => 5610
        [2] => 4943
    )

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

)

Each array is a list of categories followed by its sub categories and sub sub categories. I want to show only original patterns of numbers. so I want to remove array 2 because that pattern of numbers already exists in array 3, but I want keep array 1 because the number that follows 4937 is different to array 3. The end result should be this,

Array
(

  [1] => Array
    (
        [0] => 4937
        [1] => 4941
    )

  [3] => Array
    (
        [0] => 4937
        [1] => 5610
        [2] => 4943
    )

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

)

If you do not have multiple parents for child categories, try this:

for($i = 1; $i < count($array); $i++){
    end($array[$i-1]);
    $k = key($array[$i-1]);
    if ($array[$i-1][$k] == $array[$i][$k]) {
        unset($array[$i-1]);
    }
}
$array = array_values($array); // reindex array if you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM