Store array within a string as an array

Within the wp_posts WordPress table, I have a row stored for a post (custom post type) that literally stores the following within the post_content column:

    [test_price] => 39.00
    [test_elegibility] => Eligible
    [test_confirmation] => confirmed
    [test_id] => DUA6A44THQH7U

When I query the table via the following:

$query = $wpdb->get_results( 'SELECT * FROM wp_posts where post_type = "custom_post_type"' );

foreach($query as $q) {
    $content = $q->post_content;

The result of the var_dump for each variable above is obviously as follows:

string(1369) string(1311) 

and so on since the post_content field contains the array I’m trying to pull out.

I know I can strip / filter out the brackets, square brackets, etc and cycle through each instance to create an associative array directly from the post_content field value quite easily, which is fine.

My question is, am I missing a trick where I can simply assign the strings as arrays immediately by simply assigning them to variables? For example, using the above:

foreach($query as $q) {
$content = $q->post_content;

The result would be that each var_dump should ideally print out:

array(2) { 'te' => string(4) "werg" 'wreg' => string(6) "werefg" }

NOTE – the field values will only ever contain the arrays, there’s nothing else appended or prepended…

Read more here: Store array within a string as an array

Leave a Reply

Your email address will not be published. Required fields are marked *