Skip to content
Advertisement

PHP storing array in database

Soo i have saved a array inn my database using var_export($myarray, true);

But when im trying to get the array from the database later i cant figure out how to do it. here is what ive tried now

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

This does not work, but when i echo $henl->bforei get this

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

Soo 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: https://i.imgur.com/Snk6Pb9.png

Advertisement

Answer

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

*/
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement