473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Fill array keys if it doesn't exists

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
2 5776
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1403
by: Tom Szabo | last post by:
cTemp = oNode.cName; aStatList = "term"; window.alert(aStatList); -> "term" window.alert(aStatList.length); -> "0" the above code is going to display "term" and "0" ...
47
5024
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
4569
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
8
1857
by: PhilM | last post by:
I would like some assistance in methodology to reassign/reset array keys to consecutive numbering, while keeping in sync a second array that uses the key of the first as it's key set. If I have...
15
51648
by: deko | last post by:
What I'd like to do is create an array of values and test for existence of those values. Here's the non-working code I'm having trouble with: Dim wcSearch(4 To 7) As Integer If Me.Value =...
7
2145
by: pauld | last post by:
Ive got a series of 2 dimensional associative arrays while(list($key, $val) = each ($results)) { {foreach($val as $i=>$v) print "$i = $v"; } } but i want to define the order that the...
30
2899
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
6
1647
by: pc | last post by:
I was wondering if there is a more gracious way to write the below code: //$input is an array .I just need its 0th and 2nd key-not the 1st.I want to avoid using temp $temp=array_keys($input);...
0
415
by: mijn naam | last post by:
<mouac01@yahoo.comschreef in bericht news:55988a3d-789f-47bb-bd80-94f291c33ce6@b30g2000prf.googlegroups.com... Show what you have so far, where do you get stuck?
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.