To split a map or associative array into two maps based on some condition-say a condition on keys or values of the map-you can use the array_filter
function along with a foreach
loop. Here's an example code:
main.php508 chars21 lines
In this example, we split the original map $original_map
into two maps based on a condition on keys. The condition states that elements with keys 'orange' and 'mango' will go to $map1
and the rest will go to $map2
.
To apply a condition on values, you can modify the condition function and use ARRAY_FILTER_USE_BOTH
flag instead of ARRAY_FILTER_USE_KEY
.
gistlibby LogSnag