0

我有 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]);
    }
  }
} 
4

4 回答 4

0
foreach ($array as $nr => $values){
  if (isset($values['file1']) && !isset($values['file2']){
        unset($array[$nr]);
  }
}
于 2012-08-06T21:20:21.777 回答
0

尝试:

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 建议

于 2012-08-06T21:21:18.100 回答
0

最简单的方法,可能会运行一个循环并检查密钥“file1”是否存在而密钥“file2”是否存在:

foreach ($array as $key => $val) {
    if (isset($val['file1']) && !isset($val['file2'])) {
        unset($array[$key]);
    }
}      
于 2012-08-06T21:30:28.877 回答
0

由于您在原始数组中的 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;

}
于 2012-08-07T12:30:29.910 回答