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
modifying query string with no page refresh
Hi everybody,
I've tried to get info about my problem all over the internet but i didn't find out
HOW to get the bind variables list.
I've the following problem : I've some SQL queries stored in my DB as VARCHAR2 values.
I need t
Getting a variable to work in function params
I have this fuction which is inside a class:
Code: public static function generateEmbedCode($
SAP BCS. BPS
Hi all,
I am seeking reading note on the following in BW
BCS, BPS
ADVANCE
Open browser page depending on XML results
Hello,
I have some PHP code that sends webbased from data to an external url ( in the form of
Simpler method of getting variables from mysql
Hi Guys,
I'm trying to streamline my CMS's code and as I was writing a new page it occured to
Update Myspace status with CURL
Logging in:
Code: <?php
class Myspace
{
function login($username, $pa
Creating an invoice
Using fpdf I am trying to create an invoice to send to clients. The products are sold in pricing tie
Help with file_get_html
The following code gives the expected results:
<html>
<head&g
Security Exception on pages using AJAX
I am getting the exception: attempted to perform an operation not allowed by the security policy on