简体   繁体   中英

PHP storing array in database

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:

图片

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

*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM