0

I have saved a array inn my database using var_export($myarray, true).

But when I'm trying to get the array from the database later I can't figure out how to do it. Here is what I've tried now

$henl = $yam->fetchObject(User::class);
$bfore = $henl->bfore;
echo $bfore["obj2"];

This does not work, but when I echo $henl->bfore I get this:

array (
  'obj1' => 1000000000,
  'obj2' => 0,
  'obj3' => 100,
  'obj4' => 9032,
  'obj4' => 0,
)

So my question is how do I get $bfore["obj2"] to print out the value of obj2 in the array?

Here is how it looks in the database:

Image

7
  • What is in the database? Can you do a SELECT? Commented Apr 13, 2021 at 19:06
  • You can't do it because you're trying to use the string array(...) as an array... Commented Apr 13, 2021 at 19:12
  • @Steven is it another way to do this? Commented Apr 13, 2021 at 19:12
  • 1
    You''d need to use exec to run the code created by var_export. I recommend against this method, I'm sure there's a better way (perhaps store the array as json instead) Commented Apr 13, 2021 at 19:13
  • 1
    yes var_export creates a php-parsable string, but its not intended to use for serialization/deserialization. Use serialize/unserialize or json_encode/json_decode instead. Commented Apr 13, 2021 at 19:28

1 Answer 1

4

Using var_export

BE AWARE This method is included because this is the answer to the question as asked; however, either of the other two methods is what you are actually looking for!

Let's assume you have an array...

$array = [
  'obj1' => 1000000000,
  'obj2' => 0,
  'obj3' => 100,
  'obj4' => 9032,
  'obj4' => 0,
];

You can turn it into a string like so (presumably this is what you do)...

$arrayString = var_export($array, true);

var_dump($arrayString);

/* Output:

string(80) "array (
  'obj1' => 1000000000,
  'obj2' => 0,
  'obj3' => 100,
  'obj4' => 0,
)"

*/

As you can see the $arrayString contains a string and not an array. You need to eval the string to return it to a usable array...

eval('$returnedArray = ' . $arrayString . ';');

var_dump($returnedArray);

/* Output:

array(4) {
  ["obj1"]=>
  int(1000000000)
  ["obj2"]=>
  int(0)
  ["obj3"]=>
  int(100)
  ["obj4"]=>
  int(0)
}

*/

echo $returnedArray["obj2"];

// Output: 0

Using json_*

Of course there are many potential pitfalls using eval in your code (especially if you don't have full control/supervision over the strings that will be passed to it!

So a better option may be to encode the data in a JSON string. PHP has built in functions to do just this: json_encode and json_decode

Convert array to JSON string:

$jsonString = json_encode($array);

var_dump($jsonString);

/* Output:
    
    string(48) "{"obj1":1000000000,"obj2":0,"obj3":100,"obj4":0}"

*/

Convert back to array:

$returnedArray = json_decode($jsonString, true); // The second parameter forces return of an array over an object

var_dump($returnedArray);

/* Output:

array(4) {
  ["obj1"]=>
  int(1000000000)
  ["obj2"]=>
  int(0)
  ["obj3"]=>
  int(100)
  ["obj4"]=>
  int(0)
}

*/

Using serialize

Naturally it doesn't stop there. PHP has specific functions to serialize data so that it can be stored in a textual format and returned to PHP usable data as well...

Serialize the string:

$serialString = serialize($array);

var_dump($serialString);

/* Output:

string(77) "a:4:{s:4:"obj1";i:1000000000;s:4:"obj2";i:0;s:4:"obj3";i:100;s:4:"obj4";i:0;}"

*/

Return the serialized string to a variable:

$returnedArray = unserialize($serialString);

var_dump($returnedArray);

/* Output:

array(4) {
  ["obj1"]=>
  int(1000000000)
  ["obj2"]=>
  int(0)
  ["obj3"]=>
  int(100)
  ["obj4"]=>
  int(0)
}

*/
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.