0

Is there any easy way to insert another key value into serialized array php mysql?

My serialized codes are:

a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}

And array from it is:

Array
(
  [0] => Sample array
  [1] => Array
    (
        [0] => Apple
        [1] => Orange
   )
)

Is there any way to add [2] => mango ?

Like this:

Array
(
  [0] => Sample array
  [1] => Array
    (
        [0] => Apple
        [1] => Orange
        [2] => mango
   )
)

1 Answer 1

1

No. You don't mess with the serialized string. You have to be absolutely PERFECT in your modifications, or you corrupt the whole thing.

The easiest/safest method is to unserialize back to a native array, modify that, then re-serialize.

$foo = "a:2:etc..."
$temp = unserialize($foo);
$temp[1][2] = 'mango';
$foo = serialize($temp);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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