473,624 Members | 2,154 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.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.
Sep 8 '08 #1
2 5813
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]);
}
Sep 8 '08 #2
..oO(mo*****@ya hoo.com)
>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(m y 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]);
}
$arr = array(...);
$keys = array();
foreach ($arr as $a){
$keys = array_merge($ke ys, $a);
}
$defaults = array_fill_keys (array_keys($ke ys), 0);
foreach ($arr as &$entry) {
$entry = array_merge($de faults, $entry);
}

Micha
Sep 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1411
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" That is it somehow adds/sets the value of the intended associative array "aStatList" to "term" but beleives the length is still zero...
47
5059
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 are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
22
4598
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
8
1880
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 read a solution on php.net, I didn't understand what I was reading. At this stage of bumbling around, I am getting rather confused/frustrated. ...and getting mind around multidimensional arrays hurts :) *** note php4 only available ***
15
51666
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 = wcSearch Then Do Something Else Do Something else
7
2150
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 various $i's are printed in
30
2927
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
1654
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); print $temp;print $temp;
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
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8620
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.