On Sep 8, 2:46*am, Sjoerd <sjoer...@gmail .comwrote:
On Mon, 08 Sep 2008 00:11:38 -0700, moua...@yahoo.c om wrote:
How would I write a script that will loop thru all the keys, add keys
and set them to zero if they don't exist in the other sub-arrays.
Something like the below. *Thanks...
Loop through the array and make an array of all keys. Then loop through
the array again and add the missing keys.
Thanks, Sjoerd. I got it to work with the code below. I was thinking
about using array_fill_key to fill all the missing keys with zeros as
well but I wasn't sure if that was better than what I have below. Let
me know if there's a better way to do this. Thanks...
$arr=array(my 1st array from above);
$keys=array();
//loop and merge the sub-arrays to get all the keys
foreach($arr as $a){
$keys=array_mer ge($keys,$a);
}
//loop to add key and value if it doesn't exists
foreach($arr as $k=>$a){
foreach($keys as $k1=>$v){
if(!array_key_e xists($k1,$a)) $arr[$k][$k1]=0;
}
ksort($arr[$k]);
}