Checking name of array(?)

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Checking name of array(?)

Post by EcazS »

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?
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Checking name of array(?)

Post by bowersbros »

[syntax=php]if(array_key_exists('production',$array) == "production"){
echo "Valid";
} else {
echo "invalid";
}[/syntax]

Try that.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

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
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Checking name of array(?)

Post by Tino »

...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.
Please check out my CodeCanyon items.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Checking name of array(?)

Post by bowersbros »

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 :P

Anyhow, the fundamentals are there, as in the function to use :)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

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
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Checking name of array(?)

Post by bowersbros »

Just a side note:

$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. ;)

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
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Checking name of array(?)

Post by Tino »

Ah, yes, of course. Good catch ;)
Please check out my CodeCanyon items.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Checking name of array(?)

Post by bowersbros »

Tino wrote:Ah, yes, of course. Good catch ;)


See, once im awake I know what im on about ;)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

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
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Checking name of array(?)

Post by bowersbros »

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 ;)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Checking name of array(?)

Post by jacek »

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 :D

I prefer isset usually :)
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Checking name of array(?)

Post by bowersbros »

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 :D

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 :P
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

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
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Checking name of array(?)

Post by EcazS »

I'm checking with empty too so it shouldn't be a problem.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Checking name of array(?)

Post by jacek »

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 :P

The slowness is to do with the handling of null values, nothing to do with optimising ;)
Image
Post Reply