Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

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?


0
savetheorcas
Asked:
savetheorcas
  • 5
  • 5
  • 3
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now