【问题标题】:Removing duplicate elements from bidemensional array php从双向数组php中删除重复元素
【发布时间】:2012-08-07 20:30:29
【问题描述】:

我有 2 个数组:1 个包含名称中包含 ID 的文件名,2 个包含一些数据:

Array
(
    [0] => Array
        (
            [file] => 103135_cara.jpg
        )

    [1] => Array
        (
            [file] => 103135_corpo.jpg
        )

    [2] => Array
        (
            [file] => 103136_cara.jpg
        )

    [3] => Array
        (
            [file] => 103136_corpo.jpg
        )


Array2
(
    [0] => Array
        (
            [id] => 103137
            [nome] => Eduardo Vieira
            [sexo] => 1
            [datanascimento] => 1983-11-15
            [morada] => R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508
            [localidade] => Joinville
            [cp1] => 
            [cp2] => 
            [tlm] => 479946464
            [email] => eduardo@wetzel.com.br
            [estadocivil] => 1
            [profissao] => 7
        )

    [1] => Array
        (
            [id] => 103138
            [nome] => João Nuno Gonçalves
            [sexo] => 1
            [datanascimento] => 1984-08-13
            [morada] => Rua Elias Garcia Nº325 6D
            [localidade] => Amadora
            [cp1] => 2700
            [cp2] => 323
            [tlm] => 964359799
            [email] => joaoridebmx@yahoo.com
            [estadocivil] => 1
            [profissao] => 7
        )

我已将数组合并为:

Array3
(
    [0] => Array
        (
            [id] => 103137
            [nome] => Eduardo Vieira
            [sexo] => 1
            [datanascimento] => 1983-11-15
            [morada] => R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508
            [localidade] => Joinville
            [cp1] => 
            [cp2] => 
            [tlm] => 479946464
            [email] => eduardo@wetzel.com.br
            [estadocivil] => 1
            [profissao] => 7
            [file1] => 103137_cara.jpg
        )

    [1] => Array
        (
            [id] => 103137
            [nome] => Eduardo Vieira
            [sexo] => 1
            [datanascimento] => 1983-11-15
            [morada] => R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508
            [localidade] => Joinville
            [cp1] => 
            [cp2] => 
            [tlm] => 479946464
            [email] => eduardo@wetzel.com.br
            [estadocivil] => 1
            [profissao] => 7
            [file1] => 103137_cara.jpg
            [file2] => 103137_corpo.jpg
        )

    [2] => Array
        (
            [id] => 103138
            [nome] => João Nuno Gonçalves
            [sexo] => 1
            [datanascimento] => 1984-08-13
            [morada] => Rua Elias Garcia Nº325 6D
            [localidade] => Amadora
            [cp1] => 2700
            [cp2] => 323
            [tlm] => 964359799
            [email] => joaoridebmx@yahoo.com
            [estadocivil] => 1
            [profissao] => 7
            [file1] => 103138_cara.jpg
        )

    [3] => Array
        (
            [id] => 103138
            [nome] => João Nuno Gonçalves
            [sexo] => 1
            [datanascimento] => 1984-08-13
            [morada] => Rua Elias Garcia Nº325 6D
            [localidade] => Amadora
            [cp1] => 2700
            [cp2] => 323
            [tlm] => 964359799
            [email] => joaoridebmx@yahoo.com
            [estadocivil] => 1
            [profissao] => 7
            [file1] => 103138_cara.jpg
            [file2] => 103138_corpo.jpg
        )

我的问题是:如何删除仅包含键 'file1' 的数组元素,保留同时具有键 'file1' 和 'file2' 的数组元素

这是我用来合并数组的代码:

foreach ($ids as $val1) {
  foreach ($files as $key => $val2) {
    $cara = strpos($val2['file'], $val1['id'].'_cara');
    if ($cara !== false) {
      $val1['file1'] = $val2['file'];
      $data[] = $val1;
      unset($files[$key]);
    }
    $corpo = strpos($val2['file'], $val1['id'].'_corpo');
    if ($corpo !== false) {
      $val1['file2'] = $val2['file'];
      $data[] = $val1;
      unset($files[$key]);
    }
  }
} 

【问题讨论】:

  • 你是如何合并数组的?我真的没有看到重复条目出现的位置或数据之间的关系。我的意思是你可以只是foreach ($array as $key => $item) { if (!isset($item['file1'], $item['file2'])) { unset($array[$key]); } } - 但我认为更好的解决方案是首先阻止骗子出现。
  • 对于 array2 中的每个条目(其中包含作为 array1 中文件名一部分的 ID),array1 中可以有 1 或 2 个条目。每个 array1 元素几乎总是有 2 个文件。
  • 我已经尝试过这个合并。我想这是我的问题:($ids is array1 and $files is array2)
  • foreach ($ids as $val1) { foreach ($files as $key => $val2) { $cara = strpos($val2['file'],$val1['id'] .'_cara'); if($cara !== false) { $val1['file1'] = $val2['file']; $data[] = $val1;未设置($files[$key]); } $corpo = strpos($val2['file'],$val1['id'].'_corpo'); if($corpo !== false) { $val1['file2'] = $val2['file']; $data[] = $val1;未设置($files[$key]); } } }

标签: php arrays duplicates


【解决方案1】:
foreach ($array as $nr => $values){
  if (isset($values['file1']) && !isset($values['file2']){
        unset($array[$nr]);
  }
}

【讨论】:

    【解决方案2】:

    简单的方法,可能会运行一个循环并检查是否存在 key 'file1' 而 key 'file2' 是否存在:

    foreach ($array as $key => $val) {
        if (isset($val['file1']) && !isset($val['file2'])) {
            unset($array[$key]);
        }
    }      
    

    【讨论】:

      【解决方案3】:

      试试:

      function filter($element) {
          $result = isset($element['file1'], $element['file2']);
      
          return $result;
      }
      
      $out = array_filter($arr3, 'filter');
      var_dump($out);
      

      $arr3 是你的第三个数组

      array_filter 也接受方法作为过滤函数:ex

      array_filter($arr3, array('class_name or class instance', 'method_name'));
      

      //已编辑
      我使用了关于 isset 的 DaveRandom 建议

      【讨论】:

      • isset() 接受多个参数,$result 变量毫无意义。只需return isset($element['file1'], $element['file2']);
      • 我忘了 isset - 谢谢。在我看来,像 return isset($a, $v, $c) 这样的结构很难用调试器检查——临时变量很便宜。
      【解决方案4】:

      由于您在原始数组中的 ID 看起来是唯一的(因为它们是 ID,而且都是 ID),因此将它们用作键是有意义的。这意味着您可以轻松识别每个文件所属的元素。

      使用 ID 作为键重新索引数组很容易:

      $data = array();
      foreach ($ids as $val) {
        $data[$val['id']] = $val;
      }
      

      然后为了将文件与适当的项目相关联,您需要做的就是:

      foreach ($files as $file) {
      
        // First get the ID of the associated item
        $parts = explode('_', $file);
        $id = $parts[0];
      
        // Now add this file to the correct item
        // Find the next spare file number for this ID
        for ($i = 1; isset($data[$id]['file'.$i]); $i++) {
          continue;
        }
        // ...and add the file
        $data[$id]['file'.$i] = $file;
      
      }
      

      这将首先避免产生重复的项目。它还避免了嵌套的foreach - 通常,如果您发现自己这样做,您就做错了。这不是一个硬性规定,但它通常只有在您正在计算多个数据集的可能排列时才有用/正确的方法,而这不是您在此处所做的。

      现在,我不得不说我不会做你所做的事情并创建file1file2,而是创建一个子数组files。这将使以后循环文件变得更加容易,并且还可以避免检查以查找文件的下一个可用编号。

      所以,我会让第二个循环看起来像这样:

      foreach ($files as $file) {
      
        // First get the ID of the associated item
        $parts = explode('_', $file);
        $id = $parts[0];
      
        // Now add this file to the correct item
        if (!isset($data[$id]['files'])) {
          $data[$id]['files'] = array();
        }
        $data[$id]['files'][] = $file;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-18
        • 2013-05-20
        • 2018-02-14
        • 2011-07-03
        • 2012-02-12
        • 2010-10-13
        相关资源
        最近更新 更多