0

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?

1 Answer 1

1

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;
            }
        }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! The [0] is a bit confusing, but it is returning all the results.
Love it, thanks. Much easier to understand and get a grasp around.
you can also use foreach for applications array

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.