Connecting Tech Pros Worldwide Help | Site Map

Re: Fill array keys if it doesn't exists

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 8th, 2008, 09:55 AM
Sjoerd
Guest
 
Posts: n/a
Default Re: Fill array keys if it doesn't exists

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.

  #2  
Old September 8th, 2008, 07:05 PM
mouac01@yahoo.com
Guest
 
Posts: n/a
Default 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]);
}
  #3  
Old September 8th, 2008, 09:15 PM
Michael Fesser
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.