phparraysserializationwoocommercewpallimport

Change just 1 key in a serialised array with 3 keys using php function


I am trying to import values which are stored as a serialised array with 3 keys like this:

a:3:{i:754;s:0:"";i:753;s:0:"";i:752;s:0:"";}

Would anyone know a php function to change the value within 1 key (adjust 754) and leave the other keys as they were?

I'm using WP ALL Import and i can match my records and adjust the value for 754, but it changes the entire serialised array, deleting the other values in the other 2 keys

Thank you in advance


Solution

  • This would replace the value of array Occurance 754

    $serial = 'a:3:{i:754;s:0:"";i:753;s:0:"";i:752;s:0:"";}';
    
    $arr = unserialize($serial);
    
    $arr[754] = 'NEW';
    print_r($arr);
    echo $serial = serialize($arr);
    

    RESULT

    Array
    (
        [754] => NEW
        [753] => 
        [752] => 
    )
    a:3:{i:754;s:3:"NEW";i:753;s:0:"";i:752;s:0:"";}