0

i have Multi-Dimensional array and i have another one that have some of the id that exist in the first Multi-Dimensional

now what i need is to use filter function to filter any element that exist in the second single array

my Multi-Dimensional array is :

 $Base = array
  (
  array("aa",1),
  array("bb",2),
  array("cc",3),
  array("dd",4),
  array("ee",5),
  array("ff",6),
  array("gg",7),
  );

and the another one is :

  $child = array(1,4,5);

what i expect now to filter the array and get

$new = array
  (
  array("aa",1),
  array("dd",4),
  array("ee",5) 
 );
2
  • 1
    Have a look at array_filter. You can use a callback that has $child available to it and just return in_array($arg[1], $child). Commented Nov 10, 2015 at 17:50
  • yes that what i need but i couldn't get the logic idea to return the true value Commented Nov 10, 2015 at 17:51

1 Answer 1

1

This should work for you.

$Base = array(
  array("aa",1),
  array("bb",2),
  array("cc",3),
  array("dd",4),
  array("ee",5),
  array("ff",6),
  array("gg",7),
);
$child = array(1,4,5);

$filtered = array_filter($Base, function($arg) use ($child){
    //return if arg1 is in $child
    return in_array($arg[1], $child);
});

Demo: http://codepad.viper-7.com/qqcl3e

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.