Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Fill array keys if it doesn't exists

Sjoerd
Guest
 
Posts: n/a
#1: Sep 8 '08
On Mon, 08 Sep 2008 00:11:38 -0700, mouac01@yahoo.com wrote:
Quote:
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.

mouac01@yahoo.com
Guest
 
Posts: n/a
#2: Sep 8 '08

re: Re: Fill array keys if it doesn't exists


On Sep 8, 2:46*am, Sjoerd <sjoer...@gmail.comwrote:
Quote:
On Mon, 08 Sep 2008 00:11:38 -0700, moua...@yahoo.com wrote:
Quote:
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]);
}
Michael Fesser
Guest
 
Posts: n/a
#3: Sep 8 '08

re: Re: Fill array keys if it doesn't exists


..oO(mouac01@yahoo.com)
Quote:
>On Sep 8, 2:46*am, Sjoerd <sjoer...@gmail.comwrote:
Quote:
>On Mon, 08 Sep 2008 00:11:38 -0700, moua...@yahoo.com wrote:
Quote:
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
Closed Thread


Similar PHP bytes