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
7
Other forums
Get word number x from string?
How can I use a function to loop through a string, and "have a look at" every word in the
Show specific day of the week
I have been racking my brain trying to get this function to work properly and it is doing so for eve
Set Categories
I am creating a submission form for somebody to come in and submit products. These products are sep
how to make database item unique
Hey guys,
is it possible to do this:
I have the database item $title being pulled for
jquery validation in codeIgniter
hai i need to add client side validation to my fiels which are selected in a forloop.the fields are
Displaying image from database
Hi,
I've got a site where that's got a database behind it. Currently it has loads of items in
Help uploading .JPG
I have a problem with upload images when the ending is .JPG capitalized.. I really don't know what c
Problem in back link
I have page where i have given javascript back link but when i click on it browser give a message
PHP SUBMIT
Code: <input name="doLogin" type="image" src="images/loginsubmit.jpg
Problem with "dynamic" index page
Hi. I have one question. In my index.php page I have this kind of code:
Code: // listaa sivut