Link to home
Start Free TrialLog in
Avatar of savetheorcas
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?


SOLUTION
Avatar of Tomeeboy
Tomeeboy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If $key is "dbHost" then $$key should create the variable $dbHost.  If that doesn't work for some reason, you may need curly brackets around $key, like this:

What about something like this:

foreach($license as $key => $value) {

${$key} = $value;

}
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 :)
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'];
Avatar of savetheorcas
savetheorcas

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...
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.


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
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)
  }
}
<?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
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","vPodAPICore");
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works like a charm! Thank you both!!

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.