0

I have a PHP multidimensional array on a WP site that I'm trying to get a single value from. I looked at other solutions on here, but can't seem to get them to work. Wondering what I'm doing wrong.

Here's my code, I'm trying to get the post title out of the first entry (for example):

<?php echo $array[0]['furniture_item']['post_title']; ?>

It just breaks the page and nothing else past it loads.

Here's the array:

Array
(
    [0] => Array
        (
            [furniture_item] => WP_Post Object
                (
                    [ID] => 585
                    [post_title] => C4
                )

        )

    [1] => Array
        (
            [furniture_item] => WP_Post Object
                (
                    [ID] => 619
                    [post_title] => L2
                )

        )

    [2] => Array
        (
            [furniture_item] => WP_Post Object
                (
                    [ID] => 195
                    [post_title] => C1
                )

        )

    [3] => Array
        (
            [furniture_item] => WP_Post Object
                (
                    [ID] => 600
                    [post_title] => C10
                )

        )

)
2
  • $array[0]['furniture_item'] is an object not an array Commented Jan 11, 2018 at 23:57
  • $array[0]['furniture_item']->post_title Commented Jan 11, 2018 at 23:57

1 Answer 1

2

It's because the furniture_item is an object, so you need to use the -> syntax to get that post_title out of it. Like so:

<?php echo $array[0]['furniture_item']->post_title; ?>

As for it not displaying anything on the site after your error code, it's because you don't have WP_DEBUG turned on. Put this in your wp-config.php file:

define( 'WP_DEBUG', true );

PHP should then actually generate errors and tell you what went wrong. Read this for more on that.

Sign up to request clarification or add additional context in comments.

Comments

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.