We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Help with this array

savetheorcas
savetheorcas asked
on
Medium Priority
907 Views
Last Modified: 2012-06-27
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?


Comment
Watch Question

CERTIFIED EXPERT
Commented:
What about something like this:

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

$$key = $value;

}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
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;

}
CERTIFIED EXPERT

Commented:
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 :)
CERTIFIED EXPERT

Commented:
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'];

Author

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

Author

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


Brian BushSolutions Architect

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

Author

Commented:
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)
  }
}
Brian BushSolutions Architect

Commented:
<?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

Author

Commented:
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");
Solutions Architect
Commented:
Sorry I missed that detail.

foreach ($license as $_key => $_value) {
    define(strtoupper($_key), $_value["value"]);
}

Does that work?
BTW, you are probably better off using $_new["VPOD_SYSCORE"] for the value.
--brian

Author

Commented:
Works like a charm! Thank you both!!

Since you've both helped, I'm going to split the points between you! Thanks again!!
CERTIFIED EXPERT

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.