php - filter array after merging keys of one array -
i trying create filtered array 3 using array 1 , array 2.
array 1
array ( [title] => value [title2] => value2 [title3] => value3 )
array 2
array ( [0] => array ( [id] => 20 [title2] => value2 [othercolumn1] => othervalue1) [1] => array ( [id] => 21 [title4] => value4 [othercolumn3] => othervalue3) )
desired result after applying intersection method:
array 3
array ( [title2] => value2 )
so far unable achieve result because array 1 , 2 have non-matching structures. have tried different techniques unable compare them due structure differences.
if (!empty($data1['array2'][0])) { foreach ($data1['array2'] $key) { // $filtered= array_intersect($array1,$key); // print_r($key); } // $filtered= array_intersect($array1,$data1['array2']);// if use $data1['array2'][0] filters fine 1 row // print_r($filtered); }
any appreciated. thank you.
given arrays:
$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'));
you can filtered array this:
$merged = call_user_func_array('array_merge', $arr2); $filtered = array_intersect($arr, $merged);
if want intersect according keys can use instead:
$filtered = array_intersect_key($arr, $merged);
Comments
Post a Comment