Is there a more efficient way to code this than what I have?


Posted on 16th Feb 2014 07:03 pm by admin

I have three associative arrays.

$combinedSettings
$userSettings
$defaultSettings

My function must combine the key and value from $userSettings and $defaultSettings into the $combinedSettings array.

$userSettings will always contain more keys than $defaultSettings will ever have.

So every once in a while there is an extra key and of course it's value in the $userSettings array than in the $defaultSettings array.

If the value of a key in $userSettings is empty we then use the value for the same key in the $defaultSettings. Every key in $defaultSettings will always be present in $userSettings. $userSettings just adds a few extra keys and their values.

Below is my working code but I am wondering if there is some way to make it more efficient, faster, perhaps with less code. It looks rather clutzy to me. Rather bloated.

Code: while (list($userKey, $userValue) = each($userSettings)) {
while ($defaultKey = key($defaultSettings)) {
$defaultValue = current($defaultSettings);
if ($userKey === $defaultKey) {
if ($userValue === "") {
$combinedSettings[$defaultKey] = $defaultValue;
} else {
$combinedSettings[$userKey] = $userKey;
}
next($defaultSettings); // to allow key() to work properly above.
} else { // add userSettings setting and value to combinedSettings and leave
// defaultSettings where it is.
$combinedSettings[$userKey] = $userValue;
}
break;
}
}

Thanks.

No comments posted yet

Your Answer:

Login to answer
288 Like 7 Dislike
Previous forums Next forums
Other forums

Progress bar tracking file downloads
Hey guys, was wondering if there was a way to track how far a file has been downloaded. I know there

add text and number image
hi guys ..

ok see i want the user to add their name and pick a number then click get it and i

Code error with Index.php
Error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/runevid/public_

How can use this array as a key?
Say I have this array for example:


print_r($array);

Prints:
Code: [Select]Array

When i am Canseling the Billing Document in VF11 I am getting Shortdump
Hi Experts

When i am Canseling the Billing Document in VF11 I am getting Shortdump.

Batch update record with Pagination
Hoping someone can help me with this issue I'm having, im trying to batch update records from a resu

an interesting question
Greetings folks, got to ask an interesting question...

let say
i have created a function s

Am i doing this wrong?
Im pulling from an Oracle database some info... one of which is a field count_pnums which is a NUMBE

need help in update query
hi
i have a dynamic form. i need help in how can i use update query when values from dynamic for

Socket problem
Hello,

Earlier I posted about my problem with my socket script. It took up to 100% CPU usage.

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash