Blog

Explode into associative array

Nov 23, 2008
Category:How To PHP Programming 

Simple function to explode a string into an associative array. Works in the same way as the normal explode but takes an extra argument which is an array of keys to use for the returned associative array.

<?php
function aexplode($delim,$string,$keys){
    foreach(explode($delim,$string) as $k=>$v){
        $result[$keys[$k]]=$v;
    }
    return $result;
}
?>

Example:

print_r(aexplode("|","silly|nice|chin|thanks",array("one","two","three","four")));

Array ( 
[one] => silly
[two] => nice
[three] => chin
[four] => thanks
)

Comments