Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU
Publicité
The code in all its forms

Php filter array by columns

30 Mars 2015 , Rédigé par Ferey Cyril

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.

Publicité
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article