0

i have an array like

$newArray = $_POST[$newId];
print_r($newArray);

it prints like

Array ( [1] => Yes [2] => a [3] => b [4] => c [5] => d [6] => e [7] => f [8] => [9] => [10] => [11] => [12] => [13] => [14] => ) 

but when i try to store in in db after serializing like

serialize($newArray)

it get stored like

s:211:"Array
(
    [1] => Yes
    [2] => ab
    [3] => c
    [4] => d
    [5] => e
    [6] => f
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    [12] =>
    [13] =>
    [14] =>
)

"; 

which is a single array element in DB..how do i properly serialize the element.

3
  • 1
    You ask "how do i properly serialize the element", but the question is to you: what do you consider "proper serialization" of an array in the DB? You haven't shared your data model or expectations, so it's rather hard to answer your question. Commented Sep 7, 2010 at 18:05
  • I assume proper serialization should be a:14:{i:1;s:3:"Yes";i:2;s:2:"ab";...} Commented Sep 7, 2010 at 18:12
  • Are you sure you are correctly posting the array? What does is_array($_POST[$newId]) return? Commented Sep 7, 2010 at 18:19

3 Answers 3

4

It looks like it's serializing a string, not an array. Are you sure $newArray is an array?

The string returned from serialize starts with 's:211'. This means that a string was passed into serialize(). If an array were passed into serialize() the returned string would start with 'a:14'.

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

2 Comments

i was storing an array in a text field ....so i thing it got converted to string... how do i over come it..?
No, I meant that you are passing a string to serialize(), not an array.
0

@pradeep where you storing $newArray in textfield, store it by serialling

$arrayString = $_POST['newId'];

You will get seriallized array in $arrayString. If you want to use that array before storing in database , use unserialize , else directly store in database as that is already seriallized.

$array = unserialize($arrayString);

This will solve your problem

2 Comments

This isn't the best idea. What if one of the strings had a comma in it?
I don't think it works this way. I like the old answer better (you just can't have a string with a comma).
0

Not really sure I understand the question, if you don't want to serialize though, and if you want to pass it from a text field, maybe do custom syntax like - 1:a;b:2;c:3

then explode(';',$string); loop that and for the result, explode(':',$rows)

make the delimiters more difficult to clash

explode("[[;]]", string); // 1]]:[[b[[;]]

Comments

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.