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?


savetheorcasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TomeeboyCommented:
What about something like this:

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

$$key = $value;

}
0
TomeeboyCommented:
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;

}
0
TomeeboyCommented:
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 :)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

TomeeboyCommented:
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'];
0
savetheorcasAuthor 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...
0
savetheorcasAuthor 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.


0
Brian BushSolutions ArchitectCommented:
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
0
savetheorcasAuthor 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)
  }
}
0
Brian BushSolutions ArchitectCommented:
<?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
0
savetheorcasAuthor 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");
0
Brian BushSolutions ArchitectCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
savetheorcasAuthor Commented:
Works like a charm! Thank you both!!

Since you've both helped, I'm going to split the points between you! Thanks again!!
0
TomeeboyCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.