Return all array elements except for a given key

Votes : 3.4
I was wondering if there is a native PHP function for returning all values from an associative array that do not match a given key(s)? //source array $source = array('key_1'=>'value_1','key_2'=>'value_2','key_3'=>'value_3','key_4'=>'value_4','key_5'=>'value_5'); // Required keys $filter1 = array('key_1','key_2'); $filter2 = array('key_3','key_4','key_5'); I wish to get result as : Array( ['key_3'] => 'value_3', ['key_4'] =>'value_4', ['key_5'] =>'value_5' ) // First $filter1 array Array( ['key_1'] => 'value_1', ['key_2'] =>'value_2' ) // Second $filter_2 array

Your Answer

1 Answer

This function compatible with any php version.
function array_diff($source,$filters){
    foreach($filters as $filter){
    return $filters;


2 Answer

array_flip function use to convert $filter1 & $filter2 array values to keys, Then array_diff_key function return different with $source & new converted $filter1 or $filter2 array.
array_diff_key($source,  array_flip($filter1));

What is the codrate ? is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila