Php数组删除重复并在另一个数组中放入toghether

时间:2017-10-15 19:13:33

标签: php arrays

我有这个php数组:

Array
(
    [0] => Array
        (
            [0] => 2505
            [1] => Lima 
            [2] => Daniels
            [3] => 0996995904
            [4] => 
            [5] => 755971
            [6] => 1454284800
            [7] => Cat. A (Moto)
            [8] => 0
            [9] => 1
            [10] => 1
        )

    [1] => Array
        (
            [0] => 2505
            [1] => Lima 
            [2] => Daniels
            [3] => 0996995904
            [4] => 
            [5] => 755971
            [6] => 1454284800
            [7] => Cat. A (Moto)
            [8] => 1
            [9] => 0
            [10] => 0
        )

    [2] => Array
        (
            [0] => 2505
            [1] => Lima 
            [2] => Daniels
            [3] => 0996995904
            [4] => 
            [5] => 755971
            [6] => 1454284800
            [7] => Cat. A (Moto)
            [8] => 0
            [9] => 0
            [10] => 0
        )

    [3] => Array
        (
            [0] => 2525
            [1] => Lomarca
            [2] => Miro
            [3] => 0099778877
            [4] => 
            [5] => 768131
            [6] => 1454976000
            [7] => Cat. A (Moto)
            [8] => 1
            [9] => 0
            [10] => 0
        )

    [4] => Array
        (
            [0] => 2525
            [1] => Lomarca
            [2] => Miro
            [3] => 0099778877
            [4] => 
            [5] => 768131
            [6] => 1454976000
            [7] => Cat. A (Moto)
            [8] => 0
            [9] => 1
            [10] => 0
        )

)

我想要新的数组: 1)如果元素[0]存在于数组中我想控制元素8,9和10,如果其中一个是1,我想在最终数组中有1,但我不想拥有相同的元素数组2次(索引[0]是键)。

我的最后一个阵容是:

Array
(
    [0] => Array
        (
            [0] => 2505
            [1] => Lima 
            [2] => Daniels
            [3] => 0996995904
            [4] => 
            [5] => 755971
            [6] => 1454284800
            [7] => Cat. A (Moto)
            [8] => 1
            [9] => 1
            [10] => 1
        )

    [3] => Array
        (
            [0] => 2525
            [1] => Lomarca
            [2] => Miro
            [3] => 0099778877
            [4] => 
            [5] => 768131
            [6] => 1454976000
            [7] => Cat. A (Moto)
            [8] => 1
            [9] => 1
            [10] => 0
        )
)

我只有一次指数2505和2505以及指数8和9和10对于2505是1而指数8和9对于2525是1。

1 个答案:

答案 0 :(得分:0)

这可以完成这项任务:(假设您的第一个数组为$arrays且您所需的结果为$result

 $result = array();
 foreach($arrays as $array)
 {
      if(!isset($result[$array[0]]))
      {
           $result[$array[0]] = $array;
      }
      else
      {
           $result[$array[0]][8]  = $array[8]  == 1 ? 1 : $result[$array[0]][8];
           $result[$array[0]][9]  = $array[9]  == 1 ? 1 : $result[$array[0]][9];
           $result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
      }
 }

但无论您从哪里获取数据,例如数据库,我会使用element[0]作为密钥,因此您不会在$arrays中获得多个条目。另一种选择是创建一个保存数据的类......