By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Re: Fill array keys if it doesn't exists

P: n/a
On Mon, 08 Sep 2008 00:11:38 -0700, mo*****@yahoo.com 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.
Sep 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sep 8, 2:46*am, Sjoerd <sjoer...@gmail.comwrote:
On Mon, 08 Sep 2008 00:11:38 -0700, moua...@yahoo.com 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_merge($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_exists($k1,$a)) $arr[$k][$k1]=0;
}
ksort($arr[$k]);
}
Sep 8 '08 #2

P: n/a
..oO(mo*****@yahoo.com)
>On Sep 8, 2:46*am, Sjoerd <sjoer...@gmail.comwrote:
>On Mon, 08 Sep 2008 00:11:38 -0700, moua...@yahoo.com 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_merge($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_exists($k1,$a)) $arr[$k][$k1]=0;
}
ksort($arr[$k]);
}
$arr = array(...);
$keys = array();
foreach ($arr as $a){
$keys = array_merge($keys, $a);
}
$defaults = array_fill_keys(array_keys($keys), 0);
foreach ($arr as &$entry) {
$entry = array_merge($defaults, $entry);
}

Micha
Sep 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.