Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add data to a serialized string -

I get the serialized string from my database - e.g. a:1:{i:0;s:2:"32";} Unserialize it add a value to it e.g. 128 serialize it and save it back

But I get s:33:"a:2:{i:0;s:2:"64";i:1;s:3:"128";}";

Where did the s:33 come into it and surrounded the rest in quotes and ends with a semi colon?

    function addItem($serializedArray, $item)
    {
       $a = unserialize($serializedArray);
       $a[] = $item;
       return serialize($a);
     }

    $lp = $wpdb->get_results( $wpdb->prepare("Select MemNo from YP_Members Limit 0,    10") );

      foreach ($lp as $userid){
       $user_id = $userid->MemNo;
        $meta_value = $wpdb->get_var( $wpdb->prepare("Select meta_value from  iropiusermeta Where meta_key = 'user_iro_area' AND user_id = $user_id") );


        echo "User=".$user_id."<br />Value=".$meta_value."<br />";


                       if ($meta_value){


                            $user_iro_area = addItem($meta_value, "128");
                            update_user_meta($user_id, 'user_iro_area_new', $user_iro_area);    

                        } else {
                        // Add meta_key user_iro_area
                        echo "<b>Add New Me</b><br />";
                        $iro_group_num[] = "128";
                        $user_iro_area = serialize($iro_group_num);
                                         update_user_meta($user_id, 'user_iro_area_new', $user_iro_area);   
                        }                           


} // End of Main While Loop 
share|improve this question

1 Answer 1

your meta value is getting serialized twice, you should pass update_user_meta an array, not a serialized string. and while you're at it, get the user meta via get_user_meta and you won't have to think about serialization at all, WordPress handles all that behind the scenes if you stick to the API.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.