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
phpmailer class & pop.gmail.com?
Code: <?php
$mail->IsSMTP();
$mail->Host = "pop.gmail.com";
Dynamic Data + Sql Server 2005 Enterprise?
Hi! I have just started to learn ASP.NET, and it looks like it is quite a lot to learn. Im not reall
Pulling out some result data from MYSQL
Hey Guys,
I've built a PHP page that has a for just imagine something like registration form
restricting another login once you logout
Hello,
how can I restrict a page from login authenticating against info in a MySQL DB for a s
How can you detect variable string in a massive string?
Ok guys, i need some help with this one.
I am going to be getting a large string, like 10-20
Question about ShowWindow()
Hello....
I'm struggling with sumpin, and I hope y'all can help.
I've got a CFormView
ldap connection
We are using ldap to get user information from the domain controller. It was working before. Recentl
big pagination problem in php
<?php
$connect = mysql_connect("localhost", "root", "")
Cannot Display Array from Select Statement + Login question
Hi,
I'm new to PHP but so far so goog. I was assigend a project and I'm very close to completion.
RTF fomatting to email content
Im trying to sen an email with content is picked up from a rtf-file (file_get_contents('*.rtf'). Mai