Brian Bruman
Brian Bruman

Reputation: 913

Parsing serialized data in mysql while loop

I'm trying to parse the WordPress meta_value column in the wp_postmeta table.

meta_value row contents are in a serialized format like so:

a:1:{i:0;O:8:"stdClass":2:{s:5:"notes";s:0:"";s:12:"applications";a:5:
{s:4:"Year";O:8:"stdClass":2:
{s:4:"name";s:4:"Year";s:5:"value";s:4:"2006";}s:4:"Make";O:8:"stdClass":2:
{s:4:"name";s:4:"Make";s:5:"value";s:5:"Isuzu";}s:5:"Model";O:8:"stdClass":2:
{s:4:"name";s:5:"Model";s:5:"value";s:5:"i-280";}s:4:"Trim";O:8:"stdClass":2:
{s:4:"name";s:4:"Trim";s:5:"value";s:31:"Base Extended Cab Pickup 2-Door";}s:6:"Engine";O:8:"stdClass":2:
{s:4:"name";s:6:"Engine";s:5:"value";s:54:"2.8L 2770CC 169Cu. In. l4 GAS DOHC Naturally Aspirated";}}}}

I was able to use a while loop to unserialize the data

$results = $mysqli->query("SELECT meta_value
           FROM wp_postmeta
           WHERE meta_key = '_ebay_item_compatibility_list';");

while($row = $results->fetch_array()) {
echo '<pre>';
print_r(unserialize($row['meta_value']));
echo '</pre>';
                                      }

This gives me an output like

Array
(
    [0] => stdClass Object
        (
            [notes] => ONLY FITS 6 LUG VEHICLES 
            [applications] => Array
                (
                [Year] => stdClass Object
                    (
                        [name] => Year
                        [value] => 2008
                    )

                [Make] => stdClass Object
                    (
                        [name] => Make
                        [value] => Toyota
                    )

                [Model] => stdClass Object
                    (
                        [name] => Model
                        [value] => Tacoma
                    )

                [Trim] => stdClass Object
                    (
                        [name] => Trim
                        [value] => Base Crew Cab Pickup 4-Door
                    )

                [Engine] => stdClass Object
                    (
                        [name] => Engine
                        [value] => 2.7L 2694CC l4 GAS DOHC Naturally Aspirated
                    )

            )

    )

[1] => stdClass Object
    (
        [notes] => ONLY FITS 6 LUG VEHICLES 
        [applications] => Array
            (
                [Year] => stdClass Object
                    (
                        [name] => Year
                        [value] => 2008
                    )

                [Make] => stdClass Object
                    (
                        [name] => Make
                        [value] => Toyota
                    )

                [Model] => stdClass Object
                    (
                        [name] => Model
                        [value] => Tacoma
                    )

                [Trim] => stdClass Object
                    (
                        [name] => Trim
                        [value] => Base Crew Cab Pickup 4-Door
                    )

                [Engine] => stdClass Object
                    (
                        [name] => Engine
                        [value] => 4.0L 3956CC 241Cu. In. V6 GAS DOHC Naturally Aspirated
                    )

            )

    )

What I am trying to do is access all the value elements.

Meaning something like $year = ['Year']['value']; , $make = ['Make']['value']; etc.

How do I access these value elements while in my while loop?

Upvotes: 0

Views: 431

Answers (1)

Vody
Vody

Reputation: 78

You have a combination of stdClass objects and arrays so for array you normally use [] and for object's properties ->

Example:

while($row = $results->fetch_array()) {
    $foo = unserialize($row['meta_value']);
    $foo[0]->applications['Make']->value;

}

EDIT: you can use foreach if there are more array elements in a row

while($row = $results->fetch_array()) {
        $foo = unserialize($row['meta_value']);
        foreach ( $foo as $f) {
            foreach ( $f->applications as $application) {
                echo $application->value;
            }
        }
}

Upvotes: 1

Related Questions