0

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

3
  • You can't store an array in a table column. Commented Feb 20, 2015 at 11:01
  • If you store a string in a database, you will retrieve a string. "array ( 'Q1' => 'gravity', 'Q2' => 'm*a', )" is a string. Have a look at serialize() : you can store an array as a string, and then unserialize this string to retrieve your array. Commented Feb 20, 2015 at 11:02
  • Its indeed a string, not an array. You don't store arrays like that in a database and then convert them to real arrays on runtime. See the comment above about serialize() Commented Feb 20, 2015 at 11:03

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Use json_decode($string_from_database,TRUE); rather to get exact array instead of stdclass object.
0

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();

1 Comment

while retrieving,is_array($array) is false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.