Support » Plugins » Hacks » Adding values to multidimentional serialized array key

  • Hi, iam trying to create a wordpress plugin. I have inserted a serialized data into my wp_option table just like this

    $options = array(
    		'width=> '240px',
    		'height' => '240px ',
    'img_id' => array('1')
    		);
    	add_option('myopp',$options);

    The above code added the serialized data to my wp_option table. What I need is, I need to add multiple img_ids into img_id key like ‘img_id’ => array(‘1’, ‘2’, ‘3’, ‘4’) every time when I upload image.

    Please anybody help me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    $options['img_id'][] = $this_img_id;
    or
    array_push($options['img_id'], $this_img_id);
    for each img_id to add. Placed between get_option() and update_option() and assuming $this_img_id has been assigned the correct current value of course.

    Thread Starter najubudeen

    (@najubudeen)

    hi, First i would like to say thank you for your reply. i added this code. it didn’t add any value into img_id. i have written my code below.

    $options_val = get_option('myopp');
    
    	$this_img_id = 6;
    	$options = array_push($options_val['img_id'], $this_img_id);
    	add_option('myopp',$options);

    Please reply.

    Thread Starter najubudeen

    (@najubudeen)

    hi, First i would like to say thank you for your reply. i added this code. it didn’t add any value into img_id. i have written my code below.

    $options_val = get_option('myopp');
    
    	$this_img_id = 6;
    	$options = array_push($options_val['img_id'], $this_img_id);
    	add_option('myopp',$options);

    Please reply.

    Thread Starter najubudeen

    (@najubudeen)

    hi, First i would like to say thank you for your reply. i added this code. it didn’t add any value into img_id. i have written my code below.

    $options_val = get_option('myopp');
    
    	$this_img_id = 6;
    	$options = array_push($options_val['img_id'], $this_img_id);
    	add_option('myopp',$options);

    Please reply.

    Moderator bcworkz

    (@bcworkz)

    $options only has the size of the array you pushed a value to. You want to save $options_val. Also, you should probably use update_option() instead of add_option(). add_option is for creating a new option, update_option is for… uh, well, updating existing options. In truth, add_option just calls update_option, so not that important.

    The important thing is just to save $options_val. See http://php.net/manual/en/function.array-push.php

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding values to multidimentional serialized array key’ is closed to new replies.