从具有内置函数的数组中删除重复列

时间:2015-11-04 22:13:54

标签: php arrays function

我需要根据第0位的元素从以下数组中删除一些项目。

Big_array(256) {
  [0] => array(6) {
    [0] => string(4) "Data"
    [1] => string(15) "Ext_EBCS01_Cota"
    [2] => string(3) "CDI"
    [3] => string(3) "CDI"
    [4] => string(6) "IPCAdp"
    [5] => string(6) "IPCAdp"
  }
  [1]=> array(6) {
    [0] => string(10) "2014-05-05"
    [1] => int(100)
    [2] => int(100)
    [3] => int(100)
    [4] => int(100)
    [5] => int(100)
  }
  [2]=> array(6) {
    [0] => string(10) "2014-05-06"
    [1] => float(99.04321866850199)
    [2] => float(100.040705)
    [3] => float(100.040705)
    [4] => float(100.0218569233119)
    [5] => float(100.0218569233119)
  }

例如,Big_array在其第0个元素上有一些重复的值,它们是:

  1. $ Big_array [0] [3]
  2. $ Big_array [0] [5]
  3. 根据我需要删除的重复元素的索引:

    1. $ Big_array [0] [3]
    2. $ Big_array [1] [3]
    3. $ Big_array [2] [3]
    4. $ Big_array [0] [5]
    5. $ Big_array [1] [5]
    6. $ Big_array [2] [5]
    7. 应该重新编制其余数组,并将其键按顺序排列如下:

      Big_array(256) {
        [0] => array(6) {
          [0] => string(4) "Data"
          [1] => string(15) "Ext_EBCS01_Cota"
          [2] => string(3) "CDI"
          [3] => string(6) "IPCAdp"
        }
        [1] => array(6) {
          [0] => string(10) "2014-05-05"
          [1] => int(100)
          [2] => int(100)
          [3] => int(100)
        }
        [2] => array(6) {
          [0] => string(10) "2014-05-06"
          [1] => float(99.04321866850199)
          [2] => float(100.040705)
          [3] => float(100.0218569233119)
      

      有没有内置的PHP函数可以做到这一点?

1 个答案:

答案 0 :(得分:1)

我不认为有一个内置函数可以在一次传递中完成所有操作,但您可以使用一些数组函数和foreach循环的组合来完成:

<?php
$repeatedIndexes = array_diff_key($Big_array[0], array_unique($Big_array[0]));

foreach($Big_array as &$value) {
    $value = array_values(array_diff_key($value, $repeatedIndexes));
}
unset($value);