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
Secure pages Sessions vs. Cookies & session_destroy() help
Im new here and new to PHP, I hope you can help me with some questions.
Im writing my web ap
Fatal error: Call to a member function fetchrow() on a non-object in C:xamppht
okay i have this query and everytime i add `item_id`=? to it... it gives me the error in the title..
wav to mp3 + goDaddy
I need to convert wav files to mp3s. From what I have found so far one way to do it is to have my PH
Checking if multidimensional array is empty?
I have a for with multiple test input fields. Specifically the user submits multiple names and ages
Disable html within defined tags
I have a mysql database that allows users to enter content with html, but I also want to have a tag
To change the name of label on SAP screen XK02.
Hi All,
Can one suggest me how to change the label of an input field of a sap standard s
Socket problem
Hello,
Earlier I posted about my problem with my socket script. It took up to 100% CPU usage.
Combining Two Queries
I have two scripts that each work fine by themselves. One is a search script with a variable that de
EU VAT Package 2010
Does any one know whether SAP will be developing new reporting functionality due the new VAT rules t
Need understanding of this bit of code
Code: <?php
// WHERE clause filters
$arrSQLFilters = array();
//