1

I am trying to create filtered array 3 using array 1 and 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 the intersection method:

ARRAY 3

Array ( [title2] => value2 )

So far I am unable to achieve the result because array 1 and 2 have non-matching structures. I have tried different techniques but I am unable compare them due to structure differences.

   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);
            }

Any help would be much appreciated. Thank you.

5
  • Difference between array1 and array2 is sort of [title] => value. How do you expect getting [title2] => value2? Commented Aug 13, 2013 at 12:10
  • thankyou @Hashem Qolami i was using the wrong function . but still the problem is still there how can i get its intersection when the structures are different ? Commented Aug 13, 2013 at 12:16
  • Both arrays contain title2 and title3, so why does your filtered array only have title2? Commented Aug 13, 2013 at 12:22
  • ugh sorry it was a typo, fixed it . yes only title 2 is common in both arrays now... Commented Aug 13, 2013 at 12:24
  • So, you want to intersect on all keys which start with "title" + their values? Why do you have differently named keys? Can't they/shouldn't they all just be "title"? Commented Aug 13, 2013 at 12:28

2 Answers 2

3

Given the 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 get your filtered array with this:

$merged = call_user_func_array('array_merge', $arr2);
$filtered = array_intersect($arr, $merged);

If you want to intersect just according to the keys you can use this instead:

$filtered = array_intersect_key($arr, $merged);
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Possibly needs array_intersect_assoc or array_intersect_key though.
@deceze - I'm not sure why. My result had the key still intact if that's what you mean.
No, I mean that the OP seems to want to intersect on keys + values, or just keys. Only intersecting on values may lead to false positives here.
-1

you can remove structural difference in the following way

$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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.