我有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]);
}
}
}
答案 0 :(得分:0)
foreach ($array as $nr => $values){
if (isset($values['file1']) && !isset($values['file2']){
unset($array[$nr]);
}
}
答案 1 :(得分: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'));
//编辑
我使用了DaveRandom关于isset的建议
答案 2 :(得分:0)
简单的方法,可能是运行循环并检查密钥'file1'是否存在而密钥'file2'不是:
foreach ($array as $key => $val) {
if (isset($val['file1']) && !isset($val['file2'])) {
unset($array[$key]);
}
}
答案 3 :(得分: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
- 一般来说,如果你发现自己这样做了,你就做错了。这不是一个硬性规则,但如果你正在做的是计算多个数据集的可能排列,它通常只是有用/正确的方法,这不是你在这里做的。
现在,我必须说我不会做你已经完成的事情并创建file1
和file2
,我会创建一个子数组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;
}