Page 1 of 1

Checking name of array(?)

Posted: Mon Dec 19, 2011 9:56 pm
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?

Re: Checking name of array(?)

Posted: Mon Dec 19, 2011 10:47 pm
by bowersbros
[syntax=php]if(array_key_exists('production',$array) == "production"){
echo "Valid";
} else {
echo "invalid";
}[/syntax]

Try that.

Re: Checking name of array(?)

Posted: Tue Dec 20, 2011 6:21 am
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.

Re: Checking name of array(?)

Posted: Tue Dec 20, 2011 2:27 pm
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 :)

Re: Checking name of array(?)

Posted: Tue Dec 20, 2011 2:28 pm
by bowersbros
Just a side note:

$config should be $db in the second parameter since thats the array variable your searching

Re: Checking name of array(?)

Posted: Tue Dec 20, 2011 4:12 pm
by Tino
Ah, yes, of course. Good catch ;)

Re: Checking name of array(?)

Posted: Tue Dec 20, 2011 4:16 pm
by bowersbros
Tino wrote:Ah, yes, of course. Good catch ;)


See, once im awake I know what im on about ;)

Re: Checking name of array(?)

Posted: Tue Dec 20, 2011 4:18 pm
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 ;)

Re: Checking name of array(?)

Posted: Wed Dec 21, 2011 12:35 am
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 :)

Re: Checking name of array(?)

Posted: Wed Dec 21, 2011 7:33 am
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

Re: Checking name of array(?)

Posted: Wed Dec 21, 2011 1:18 pm
by EcazS
I'm checking with empty too so it shouldn't be a problem.

Re: Checking name of array(?)

Posted: Wed Dec 21, 2011 4:00 pm
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 ;)