0

I am trying to add the following array into the meta post table

"a:2:{s:11:"target";a:1:{i:0;a:1:{i:1;a:3:{s:5:"param";s:13:"page_selected";s:8:"operator";s:2:"==";s:5:"value";a:5:{i:3001;s:13:"Service Areas";i:1098;s:19:"Water Slide Rentals";i:672;s:15:"Chairs & Tables";i:205;s:4:"Home";i:1401;s:29:"Bounce House Rental Armada MI";}}}}s:15:"sgpb-conditions";N;}"

I broke down the array as follows

a:2:{  ##  ARRAY 1 [3 ELEMENTS]<br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 1-1<br>
s:11:"target";&nbsp;&nbsp;&nbsp;a:1:{ ##  ARRAY 2 [2 ELEMENTS]<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 1-2 <br>
   i:0;&nbsp;&nbsp;&nbsp;a:1:{    ##  ARRAY 3 [2 ELEMENTS]<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 2-1 <br>
   i:1;&nbsp;&nbsp;&nbsp;a:3:{ ##  ARRAY 3 [4 ELEMENTS]<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 3-1 <br>
s:5:"param";&nbsp;&nbsp;&nbsp;s:13:"page_selected";<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 3-2 <br>
s:8:"operator";&nbsp;&nbsp;&nbsp;s:2:"==";<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 3-3 <br>
s:5:"value";&nbsp;&nbsp;&nbsp;a:5:{ ##  ARRAY 4 [6 ELEMENTS]<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 4-1 <br>
i:3001;&nbsp;&nbsp;&nbsp;s:13:"Service Areas";<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 4-2 <br>
i:1098;&nbsp;&nbsp;&nbsp;s:19:"Water Slide Rentals";<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 4-3 <br>
i:672;&nbsp;&nbsp;&nbsp;s:15:"Chairs & Tables";<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 4-4 <br>
i:205;&nbsp;&nbsp;&nbsp;s:4:"Home";<br><br>
 -->KEY&nbsp;&nbsp;&nbsp;VALUE 4-5 <br>
i:1401;&nbsp;&nbsp;&nbsp;s:29:"Bounce House Rental Armada MI";<br><br>

} <!-- ENDS ARRAY 5 --><br>
} <!-- ENDS ARRAY 4 --><br>
} <!-- ENDS ARRAY 3 --><br>
} <!-- ENDS ARRAY 2 --><br>
s:15:"sgpb-conditions";N;}  <!-- ENDS ARRAY 1 --><br>

^^^^^^^^^^^^^^^^^^^^^^^^^^

NOT SURE HOW THIS TIES IN

my question is about First issue I am having is a complete understanding of the array. Not sure how the last values tie into an array. Also, an Array is zero based correct?

Here is the code I have created so far to add the array, which is not creating the array correctly.

"<?php<br>
$poddata = Array(<br>
'sgpb-target',<br>
Array (<br>
    1 => Array(<br>
'param' => 'page_selected',<br>
'operator' => '==',<br>
'value' => Array(<br>
                3001 => 'Service Areas',<br>
                1098 => 'Water Slide Rentals',<br>
                672 => 'Chairs & Tables',<br>
                1401 => 'Bounce House Rental Armada MI',<br>
                676 => 'Mobile Gaming Trucks'<br>
                ))));<br>
<br>
update_post_meta(6668, 'poddata', $poddata);<br>
?>"  

Thanks for any help

2
  • Why does your last block of code have <br> tags in it? Commented Feb 27, 2019 at 10:50
  • Bad post editing, I needed to wrap it in [code][/code] tags, and not use html tags to format the post Commented Feb 27, 2019 at 12:58

1 Answer 1

0

That original value is a serialised array. If you want to see the original array you can use the unserialize() function, or a site like https://www.unserialize.com. The problem is that this isn't working for your Array, because the serialised value is invalid.

The problem is here:

s:11:"target"

"Target" is 6 letters, so this needs to be:

s:6:"target"

This wouldn't happen if you were properly serialising and unserialising in PHP, but it's an easy mistake to make if you're trying to manually edit the data. Generally it's a bad idea to try and edit serialised data by hand.

Anyway, I assume that you want to insert a valid array, so when you fix that issue and then unserialise it, it looks like this:

array(
    'target' => array(
        0 => array (
            1 => array(
                'param'    => 'page_selected',
                'operator' => '==',
                'value'    => array(
                    3001 => 'Service Areas',
                    1098 => 'Water Slide Rentals',
                    672  => 'Chairs & Tables',
                    205  => 'Home',
                    1401 => 'Bounce House Rental Armada MI',
                ),
            ),
        ),
    ),
    'sgpb-conditions' => NULL,
)
1
  • I did try to manual recreate the array and manual inject it into the database, but like you said bad idea. Than I found "update_post_meta" which did insert a clean array but structured incorrectly. The array structure you provided worked perfect! Thanks for your help and cleaning up my post. Commented Feb 27, 2019 at 12:50

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.