Sandeep Vellaparambil
Sandeep Vellaparambil

Reputation: 15

string to array in php not possible while retieving from database

when i use this code

$a=array ( 'Q1' => 'gravity', 'Q2' => 'm*a',);

print_r($a); output will be array

$a is an array

suppose array ( 'Q1' => 'gravity', 'Q2' => 'm*a', ) is stored in a table column

when retrieved this column values and stored in a variable then that variable is not an array

Upvotes: 0

Views: 54

Answers (2)

Jasper
Jasper

Reputation: 389

You can store array in to database using this one

$a=array ( 'Q1' => 'gravity', 'Q2' => 'm*a',);
$arrstr=mysql_escape_string(serialize($a));

Retrived by this function

$array= unserialize();

Upvotes: 0

zafirov
zafirov

Reputation: 392

before you save in db try:

json_encode($a);

result is json string, You should save that string in database. Then when you get from db just json decode that string.

json_decode($string_from_database);

Upvotes: 1

Related Questions