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
Did you know?Explore Trending and Topic pages for more stories like this.
$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
PHP webpage & array print issue
I have this code running, and it works perfectly … however, see my bottom bit about what I see
if php cookie set, show code...
Hi all.. I need to figure out this little snippet right quick.. seems like it should be easy enough
Saving PHP output as a file
I wondered if anyone knows how to save the rendered output of a PHP script as a file?
I have
RSS poster script?
I have found a script that posts RSS's for me on a site that I'm building. However I would like to a
Consuming third party Payment Gateway API from Procedure.
Hi All,
First of all i would like to thank all the people of the oracle forum for providi
Multiple submit forms inside a mysql array page
Hi all,
Im having trouble to get the beneath script to work the thing i can't seem to get wor
Error: SQL Syntax; Line 1
Code:
<?php require "global_settings.php"; ?>
<title&g
Spliting paragraph into sentences and attach in
Here is what I am trying to do
example: Para1[123.456.789!] 3 sentences
Para2
Structure Question - One Table or One Table Per Record Set?
I have a web app (mySQL and PHP) which allows people to create an item with up to 200 records which
Multidimensional Array into an Html table, help!
Hello all, I am new to HTML/PHP so any help would be great.
I have a multidimensional array f