473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5769
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
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
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
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
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
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
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
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
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
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?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.