savetheorcas
asked on
Help with this array
I know this solution is going to be simple, but I've spent all day trying to figure out how do get this to work and just can't wrap my brain around it.
I have a script which requires a license in order to work. The license contains certain information which I need my script to retrieve and define as a variable. The information in the license is stored as an array, so what I need to do is something like this:
$license = ioncube_license_properties ();
foreach($key as $key => $value) {
define($key,$value);
}
The defined variable will be used elsewhere in the program, with other pages including the file that includes the above code.
If I use this code, this is the output:
<code>
$license = ioncube_license_properties ();
print_r($license);
</code>
Array (
[dbHost] => Array ( [value] => localhost [enforced] => )
[dbType] => Array ( [value] => mysql [enforced] => )
[dbUserName] => Array ( [value] => root [enforced] => )
[dbUserPass] => Array ( [value] => password [enforced] => )
[stoMySQLHost] => Array ( [value] => [enforced] => )
[stoMySQLUser] => Array ( [value] => [enforced] => )
[stoMysqlPass] => Array ( [value] => [enforced] => )
)
I've tried all sorts of combinations of foreach, while, etc and the closest I've come to getting this write is:
dbHost => Array
What am I doing wrong here?
I have a script which requires a license in order to work. The license contains certain information which I need my script to retrieve and define as a variable. The information in the license is stored as an array, so what I need to do is something like this:
$license = ioncube_license_properties
foreach($key as $key => $value) {
define($key,$value);
}
The defined variable will be used elsewhere in the program, with other pages including the file that includes the above code.
If I use this code, this is the output:
<code>
$license = ioncube_license_properties
print_r($license);
</code>
Array (
[dbHost] => Array ( [value] => localhost [enforced] => )
[dbType] => Array ( [value] => mysql [enforced] => )
[dbUserName] => Array ( [value] => root [enforced] => )
[dbUserPass] => Array ( [value] => password [enforced] => )
[stoMySQLHost] => Array ( [value] => [enforced] => )
[stoMySQLUser] => Array ( [value] => [enforced] => )
[stoMysqlPass] => Array ( [value] => [enforced] => )
)
I've tried all sorts of combinations of foreach, while, etc and the closest I've come to getting this write is:
dbHost => Array
What am I doing wrong here?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Yay for pasting my whole first post along with the last example... heh.
Anyway, that should work for you. Hope it's what you're looking for :)
Anyway, that should work for you. Hope it's what you're looking for :)
Just noticed that $value is going to be an array each time with the keys 'value' and 'enforced'. I don't know what the 'enforced' key contains, but if you just want to get the 'value' then change the code inside the loop to this:
${$key} = $value['value'];
${$key} = $value['value'];
ASKER
Well, we're getting closer.
This:
$license = ioncube_license_properties ();
foreach($license as $key => $value) {
echo ${$key} = $value['value'];
}
Produces this:
localhostmysqlrootpassword
So, now the values are displaying wthout keys.
So, what I need now is to be able to get both the keys and the values and take those key/value pairs and put them into define statements.
define($key,$value);
Etc...
This:
$license = ioncube_license_properties
foreach($license as $key => $value) {
echo ${$key} = $value['value'];
}
Produces this:
localhostmysqlrootpassword
So, now the values are displaying wthout keys.
So, what I need now is to be able to get both the keys and the values and take those key/value pairs and put them into define statements.
define($key,$value);
Etc...
ASKER
Okay, got one step further...
<code>
$license = ioncube_license_properties ();
foreach($license as $key => $value) {
echo "Key ".$key." has value " .${$key} = $value['value']."<br />";
}
</code>
Prints this:
Key dbHost has value localhost
Key dbType has value mysql
Key dbUserName has value root
Key dbUserPass has value password
Key stoMySQLHost has value
Key stoMySQLUser has value
Key stoMysqlPass has value
So now I'm getting both keys and values, now I just need help getting these into workable define statements.
<code>
$license = ioncube_license_properties
foreach($license as $key => $value) {
echo "Key ".$key." has value " .${$key} = $value['value']."<br />";
}
</code>
Prints this:
Key dbHost has value localhost
Key dbType has value mysql
Key dbUserName has value root
Key dbUserPass has value password
Key stoMySQLHost has value
Key stoMySQLUser has value
Key stoMysqlPass has value
So now I'm getting both keys and values, now I just need help getting these into workable define statements.
Do you mind posting the output of this:
echo "<PRE>\n";
var_dump($license);
echo "</PRE>\n";
Change anything sensitive, but try to leave it readable.
--brian
echo "<PRE>\n";
var_dump($license);
echo "</PRE>\n";
Change anything sensitive, but try to leave it readable.
--brian
ASKER
Nothing sensitive here, here's the full output:
array(7) {
["dbHost"]=>
array(2) {
["value"]=>
string(9) "localhost"
["enforced"]=>
bool(false)
}
["dbType"]=>
array(2) {
["value"]=>
string(5) "mysql"
["enforced"]=>
bool(false)
}
["dbUserName"]=>
array(2) {
["value"]=>
string(4) "root"
["enforced"]=>
bool(false)
}
["dbUserPass"]=>
array(2) {
["value"]=>
string(8) "password"
["enforced"]=>
bool(false)
}
["stoMySQLHost"]=>
array(2) {
["value"]=>
string(0) ""
["enforced"]=>
bool(false)
}
["stoMySQLUser"]=>
array(2) {
["value"]=>
string(0) ""
["enforced"]=>
bool(false)
}
["stoMysqlPass"]=>
array(2) {
["value"]=>
string(0) ""
["enforced"]=>
bool(false)
}
}
array(7) {
["dbHost"]=>
array(2) {
["value"]=>
string(9) "localhost"
["enforced"]=>
bool(false)
}
["dbType"]=>
array(2) {
["value"]=>
string(5) "mysql"
["enforced"]=>
bool(false)
}
["dbUserName"]=>
array(2) {
["value"]=>
string(4) "root"
["enforced"]=>
bool(false)
}
["dbUserPass"]=>
array(2) {
["value"]=>
string(8) "password"
["enforced"]=>
bool(false)
}
["stoMySQLHost"]=>
array(2) {
["value"]=>
string(0) ""
["enforced"]=>
bool(false)
}
["stoMySQLUser"]=>
array(2) {
["value"]=>
string(0) ""
["enforced"]=>
bool(false)
}
["stoMysqlPass"]=>
array(2) {
["value"]=>
string(0) ""
["enforced"]=>
bool(false)
}
}
<?php
$_new = array();
foreach ($license as $_key => $_value) {
$_new[$_key] = $_value["value"];
}
echo "<PRE>\n";
var_dump($_new);
echo "</PRE>\n";
?>
Is that what you wanted?
--brian
$_new = array();
foreach ($license as $_key => $_value) {
$_new[$_key] = $_value["value"];
}
echo "<PRE>\n";
var_dump($_new);
echo "</PRE>\n";
?>
Is that what you wanted?
--brian
ASKER
Well, sort of...
Certain information will be included in the license file such as the username and password the client (user) has been assigned to access our mySQL server. I want to be able to take this information from the license and define them as constants so that can be easily referenced throughout the application.
So, I need a way to take the key/value pairs in the license file and define them as constants such as:
define("VPOD_SYSCORE","vPo dAPICore") ;
Certain information will be included in the license file such as the username and password the client (user) has been assigned to access our mySQL server. I want to be able to take this information from the license and define them as constants so that can be easily referenced throughout the application.
So, I need a way to take the key/value pairs in the license file and define them as constants such as:
define("VPOD_SYSCORE","vPo
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Works like a charm! Thank you both!!
Since you've both helped, I'm going to split the points between you! Thanks again!!
Since you've both helped, I'm going to split the points between you! Thanks again!!
I'm not sure why you need these to be defined as constants... Using my original method, it created variables for all of the data that could easily be used throughout the script... am I missing something?
foreach($license as $key => $value) {
${$key} = $value;
}
Would create the variables:
$dbHost = "localhost";
$dbType = "mysql";
$dbUserName = "root";
$dbUserPass has = "password";
... etc.
foreach($license as $key => $value) {
${$key} = $value;
}
Would create the variables:
$dbHost = "localhost";
$dbType = "mysql";
$dbUserName = "root";
$dbUserPass has = "password";
... etc.
What about something like this:
foreach($license as $key => $value) {
${$key} = $value;
}