Php filter array by columns
Convert big array to smaller
I have a big array to read or a really big array to copy severall time .... in memory and finaly I use only four columns. That a usualy case, while it's so simple to avoid extra informations.
Use filter
Example
$List[] = array("a"=>"Apple","b"=>"Bananas","c"=>"Caroub","d"=>"diops","e"=>"electron","f"=>"first");
$Filter = array("a","d","f");
foreach($List as $ListElt) {
$ListFilter[] = array_diff_key($ListElt, array_flip($Filter));
}
Output an array
[0]
"a"=>"Apple"
"d"=>"diops"
"f"=>"first"
Function use
array_diff_keys
Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
array_flip
returns an array in flip order, i.e. keys from array become values and values from array become keys.