tags:

views:

60

answers:

3

Hi, 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.

+4  A: 

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'.

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

@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

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

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[[;]]
doingsitups