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
How to Create a Dynamic table
col1 col2 date1 date2 date3 date4..........
a b v1 v2 v3 v4
Simple MySQL query...
Hello,
How could I do a mysql query that does this: SELECT * WHERE date/time < 5minutes ag
Change of partner analyse in sales orders
Hello,
I changed the partner analyse for sales orders and added a new mandatory partnerro
fwrite error
Hi All,
Does anyone know what is causing the error in this code?
Code: <?
$error
Stumped by Third Day of Month problem
I am trying to build an algorithm that will essentially do this:
Code: [Select]$this_month = Oct
cPanel API
Hi Guys,
Need a little guidance. I'm trying to get my script to communicate with cPanel (or W
Making multiple rows with one query
I have a table called efed_handler_characters with two fields handler_id and bio_id. What query 2 is
Insert to MySQL inside foreach
Hey.
I am parsing some html, and putting inside an html db.
I need to create a randomized inte
Code doesn't print what i wanted it too. Please help!
Hi there,
I wrote this code to mae it so that in the form before it that the user said their
Pass sql into pl/sql and create RMAN duplicate script.
Hi,
I'm new to pl/sql and I'm trying to write a script that will generate some RMAN comma