Rejoanul Alam Rejoanul Alam - 3 months ago 7
PHP Question

php filter an array by another keys of array

I have two array

$x = array(1 => 'test1', 2 => 'test', 3 => 'test3', 4 => 'test4');
$y = array(1,4);


I want to know how can I filter
$x
by
$y
(array of keys). So my desired result is (after filter)

$z = array(2 => 'test', 3 => 'test3');


These arrays will be large. So unset one by one is not efficient here. I want any array function to do that. Is it possible??

Answer

Try this:

<?php

$x = array(1 => 'test1', 2 => 'test', 3 => 'test3', 4 => 'test4');
$y = array(1,4);

$result=array_diff_key($x,array_flip($y));

print_r($result);