Checking name of array(?)
Checking name of array(?)
I don't really know how to explain this so let's just dive into it.
I have an array like this in my config file,
[syntax=php]
$db["default"]["hostname"] = "localhost";
$db["default"]["username"] = "root";
$db["default"]["password"] = "pass";
$db["default"]["database"] = "db";
[/syntax]
but here is the thing, I want to be able to "save" different "connections". So for example, I could have like this,
[syntax=php]
$db["development"]["hostname"] = "localhost";
$db["development"]["username"] = "root";
$db["development"]["password"] = "pass";
$db["development"]["database"] = "devDB";
$db["production"]["hostname"] = "localhost";
$db["production"]["username"] = "root";
$db["production"]["password"] = "pass";
$db["production"]["database"] = "proDB";
[/syntax]
And then I have this line,
[syntax=php]$config["conRoute"] = "default";[/syntax]
in my config file too.
So basically I can choose different connections but I can set up multiple in the config file and just change the route variable if I wanna use another connection. I've already fixed it so it throws an error when the conRoute variable is empty but how would I check if the conRoute value is a db variable?
So with the example above, if the conRoute is "development" or "production" then it would be valid but if it was "online" then it would be invalid because that setup doesn't exist. How would I do this?
I have an array like this in my config file,
[syntax=php]
$db["default"]["hostname"] = "localhost";
$db["default"]["username"] = "root";
$db["default"]["password"] = "pass";
$db["default"]["database"] = "db";
[/syntax]
but here is the thing, I want to be able to "save" different "connections". So for example, I could have like this,
[syntax=php]
$db["development"]["hostname"] = "localhost";
$db["development"]["username"] = "root";
$db["development"]["password"] = "pass";
$db["development"]["database"] = "devDB";
$db["production"]["hostname"] = "localhost";
$db["production"]["username"] = "root";
$db["production"]["password"] = "pass";
$db["production"]["database"] = "proDB";
[/syntax]
And then I have this line,
[syntax=php]$config["conRoute"] = "default";[/syntax]
in my config file too.
So basically I can choose different connections but I can set up multiple in the config file and just change the route variable if I wanna use another connection. I've already fixed it so it throws an error when the conRoute variable is empty but how would I check if the conRoute value is a db variable?
So with the example above, if the conRoute is "development" or "production" then it would be valid but if it was "online" then it would be invalid because that setup doesn't exist. How would I do this?
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: Checking name of array(?)
[syntax=php]if(array_key_exists('production',$array) == "production"){
echo "Valid";
} else {
echo "invalid";
}[/syntax]
Try that.
echo "Valid";
} else {
echo "invalid";
}[/syntax]
Try that.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Re: Checking name of array(?)
...That doesn't make sense. The use of array_key_exists does, but the way you did doesn't ![Confused :?](./images/smilies/icon_e_confused.gif)
Either way, yes, with array_key_exists, you can check if an array contains a certain key. So you can check if the value of $config['conRoute'] is a key in an array using this function.
[syntax=php]if ( array_key_exists($config['conRoute'], $config) ) {
echo 'it exists!';
} else {
echo 'it does not exist!';
}[/syntax]
It's a very useful function.
![Confused :?](./images/smilies/icon_e_confused.gif)
Either way, yes, with array_key_exists, you can check if an array contains a certain key. So you can check if the value of $config['conRoute'] is a key in an array using this function.
[syntax=php]if ( array_key_exists($config['conRoute'], $config) ) {
echo 'it exists!';
} else {
echo 'it does not exist!';
}[/syntax]
It's a very useful function.
Please check out my CodeCanyon items.
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: Checking name of array(?)
Tino wrote:...That doesn't make sense. The use of array_key_exists does, but the way you did doesn't
Either way, yes, with array_key_exists, you can check if an array contains a certain key. So you can check if the value of $config['conRoute'] is a key in an array using this function.
[syntax=php]if ( array_key_exists($config['conRoute'], $config) ) {
echo 'it exists!';
} else {
echo 'it does not exist!';
}[/syntax]
It's a very useful function.
My bad, i was very tired. And looking at the time i posted it, it was 11:47 PM so.. that might explain abit of it
![Razz :P](./images/smilies/icon_razz.gif)
Anyhow, the fundamentals are there, as in the function to use
![Smile :)](./images/smilies/icon_e_smile.gif)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: Checking name of array(?)
Just a side note:
$config should be $db in the second parameter since thats the array variable your searching
$config should be $db in the second parameter since thats the array variable your searching
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: Checking name of array(?)
Tino wrote:Ah, yes, of course. Good catch
See, once im awake I know what im on about
![Wink ;)](./images/smilies/icon_e_wink.gif)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: Checking name of array(?)
On a side note, you could also do this:
[syntax=php]if(isset($db[$config['conRoute']]) === true) { echo "True";} else { echo "False";}[/syntax]
But the other way is probably more efficient![Wink ;)](./images/smilies/icon_e_wink.gif)
[syntax=php]if(isset($db[$config['conRoute']]) === true) { echo "True";} else { echo "False";}[/syntax]
But the other way is probably more efficient
![Wink ;)](./images/smilies/icon_e_wink.gif)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Re: Checking name of array(?)
bowersbros wrote:But the other way is probably more efficient
isset is about 33% faster compared to array_key_exists. But if the array value is null isset will return false (which it shouldn't since the array element does exists), whereas array_key_exists will return true like it should
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
I prefer isset usually
![Smile :)](./images/smilies/icon_e_smile.gif)
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: Checking name of array(?)
jacek wrote:bowersbros wrote:But the other way is probably more efficient
isset is about 33% faster compared to array_key_exists. But if the array value is null isset will return false (which it shouldn't since the array element does exists), whereas array_key_exists will return true like it should
I prefer isset usually
Really? I would have thought they'd have optimised that code since you know.. Its written in C and not PHP.
Ah well, good to know
![Razz :P](./images/smilies/icon_razz.gif)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
![Wink ;)](./images/smilies/icon_e_wink.gif)
Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Re: Checking name of array(?)
I'm checking with empty too so it shouldn't be a problem.
Re: Checking name of array(?)
bowersbros wrote:Really? I would have thought they'd have optimised that code since you know.. Its written in C and not PHP.
Ah well, good to know
The slowness is to do with the handling of null values, nothing to do with optimising
![Wink ;)](./images/smilies/icon_e_wink.gif)