user1048676
user1048676

Reputation: 10066

seralize multidimensional array

I have the following value in my database:

a:2:{s:4:"ques";a:2:{i:0;i:15;i:1;i:17;}s:5:"marks";a:2:{i:0;s:1:"2";i:1;s:1:"2";}}

I tried to do the following to get the value into my database:

$question["ques"] = '15';
$question["ques"] = '17';
$question["marks"] = '1';
$question["marks"] = '1';

$questions = serialize($question);
update_post_meta($quiz_id, 'quiz_questions1', $questions);

This doesn't update the database correctly. The output I get using this is the following:

a:2:{s:4:"ques";s:2:"17";s:5:"marks";s:1:"1";}

Any ideas how to do this?

Upvotes: 0

Views: 59

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

If you have a serialized data structure then the simplest way to understand it is to unserialize it, and show it to yourself with a print_r().

<?php
$s = 'a:2:{s:4:"ques";a:2:{i:0;i:15;i:1;i:17;}s:5:"marks";a:2:{i:0;s:1:"2";i:1;s:1:"2";}}';

$array = unserialize($s);
print_r($array);

Which will show

Array
(
    [ques] => Array
        (
            [0] => 15
            [1] => 17
        )

    [marks] => Array
        (
            [0] => 2
            [1] => 2
        )
)

So now you just have to duplicate that structure with your new data

$array = array();
$array['ques'] = array(15, 17);
$array['marks'] = array('1', '1');

// take a look to check
print_r($array);

// and if it is good. Serialize it and store it on the database
$questions = serialize($array);
update_post_meta($quiz_id, 'quiz_questions1', $questions);

Upvotes: 3

Related Questions