Php 合并一个数组的键后筛选数组
我正在尝试使用数组1和数组2创建过滤的数组3 阵列1Php 合并一个数组的键后筛选数组,php,arrays,Php,Arrays,我正在尝试使用数组1和数组2创建过滤的数组3 阵列1 Array ( [title] => value [title2] => value2 [title3] => value3 ) 阵列2 Array ( [0] => Array ( [id] => 20 [title2] => value2 [otherColumn1] => otherValue1
Array (
[title] => value
[title2] => value2
[title3] => value3
)
阵列2
Array (
[0] => Array ( [id] => 20 [title2] => value2 [otherColumn1] => otherValue1)
[1] => Array ( [id] => 21 [title4] => value4 [otherColumn3] => otherValue3)
)
应用交叉方法后的预期结果:
阵列3
Array ( [title2] => value2 )
到目前为止,我无法获得结果,因为数组1和2的结构不匹配。我尝试过不同的技术,但由于结构差异,我无法比较它们
if (!empty($data1['array2'][0])) {
foreach ($data1['array2'] as $key) {
// $filtered= array_intersect($array1,$key);
// print_r($key);
}
// $filtered= array_intersect($array1,$data1['array2']);// if i use $data1['array2'][0] it filters fine but just one row
// print_r($filtered);
}
任何帮助都将不胜感激。谢谢。鉴于阵列:
$arr = array('title' => 'value', 'title2' => 'value2', 'title3' => 'value3');
$arr2 = array (
0 => array ( 'id' => '20', 'title2' => 'value2', 'otherColumn1' => 'otherValue1'),
1 => array ( 'id' => '21', 'title4' => 'value4', 'otherColumn3' => 'otherValue3'));
您可以通过以下方式获得过滤后的阵列:
$merged = call_user_func_array('array_merge', $arr2);
$filtered = array_intersect($arr, $merged);
如果只想根据关键点相交,可以使用以下选项:
$filtered = array_intersect_key($arr, $merged);
可以通过以下方式删除结构差异
$arr1 = array (
0 => array('title' => 'value', 'title2' => 'value2', 'title3' => 'value3'));
$arr2 = array (
0 => array ( 'id' => '20', 'title2' => 'value2', 'otherColumn1' => 'otherValue1'),
1 => array ( 'id' => '21', 'title4' => 'value4', 'otherColumn3' => 'otherValue3'));
array1
和array2
之间的区别有点像[title]=>值。您希望如何获得[title2]=>value2
?thankyou@Hashem Qolami我使用了错误的函数。但问题仍然存在,当结构不同时,如何获得其交集?两个数组都包含title2和title3,那么为什么过滤数组只有title2?呃,很抱歉,这是一个拼写错误,已修复。是的,现在只有标题2在这两个数组中是通用的……那么,您想在所有以“title
”开头的键及其值上相交吗?为什么有不同名称的密钥?它们不能/不应该都是“title
”?+1可能需要array\u intersect\u assoc
或array\u intersect\u key
。@deceze-我不确定为什么。如果你是这个意思的话,我的结果是键仍然完整。不,我的意思是OP似乎想要在键+值上相交,或者只在键上相交。此处仅值相交可能导致误报。