note 89126 modified in function.array-intersect-key by danbrown

From: Date: Mon, 23 Feb 2009 16:16:42 +0000
Subject: note 89126 modified in function.array-intersect-key by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151017@lists.php.net to get a copy of this message
Just a simple script if you want to use one array, which contains only zeros and ones, as mask for another one (both arrays must have the same size of course). $outcome is an array that contains only those values from $source where $mask is equal to 1. <?php $outcome = array_values(array_intersect_key( array_values($source), array_filter(array_values($mask)) )); ?> PS: the array_values() function is necessary to ensure that both arrays have the same numbering/keys, otherwise your masking does not behave as you expect. Enjoy! --was-- Just a simple script if you want to use one array, which contains only zeros and ones, as mask for another one (both arrays must have the same size of course). $outcome is an array that contains only those values from $source where $mask is equal to 1. $outcome = array_values(array_intersect_key( array_values($source), array_filter(array_values($mask)) )); PS: the array_values() function is necessary to ensure that both arrays have the same numbering/keys, otherwise your masking does not behave as you expect. Enjoy! http://php.net/manual/en/function.array-intersect-key.php

« previous php.notes (#151017) next »