PHP-具有相同值和不同键的数组 - 启用

时间:2018-06-01 17:19:12

标签: php arrays multidimensional-array

如果我在php中的不同键上有相同的值会发生什么?所以我需要删除其他有价值的键并且只保留一个。这两个数组可以是单数组也可以是多维数组。 请看一下代码

Array
(
      [success] => Array
         (
            [0] => Done.
            [1] => Done.
         )

      [error] => Array
         (
            [0] => This request has already been processed.
            [1] => This request has already been processed.
            [2] => This request has already been processed.
            [3] => You Don't Have Permission.
            [4] => You Don't Have Permission.
         )

      [warning] => Array
         (
         )

)

正如您在[error][success]数组中看到的那样,不同的键上有2/3相同的值。 现在我该怎么办?只保留一个。 喜欢下面的一个

Array
(
      [success] => Array
         (
            [0] => Done.
         )

      [error] => Array
         (
            [0] => This request has already been processed.
            [1] => You Don't Have Permission.
         )

      [warning] => Array
         (
         )

)

1 个答案:

答案 0 :(得分:5)

对于您的示例数据,您可以将array_maparray_unique一起使用。

$result = array_map("array_unique", $arrays);