简体   繁体   中英

iterate through unserialized wordpress options array in php

I am trying to ultimately grab the wordpress plugin slug and new version values from this object array. I've done a lot of research and tried many examples but none really show this array format. I am confused by the index structure. Help is appreciated.

Here is a snippet of the array structure from the unserialized wordpress data.

stdClass Object
(
    [last_checked] => 1594309667
    [response] => Array
        (
            [advanced-custom-fields/acf.php] => stdClass Object
                (
                    [id] => w.org/plugins/advanced-custom-fields
                    [slug] => advanced-custom-fields
                    [plugin] => advanced-custom-fields/acf.php
                    [new_version] => 5.8.12
                    [url] => https://wordpress.org/plugins/advanced-custom-fields/
                    [package] => https://downloads.wordpress.org/plugin/advanced-custom-fields.5.8.12.zip
                    [icons] => Array
                        (
                            [2x] => https://ps.w.org/advanced-custom-fields/assets/icon-256x256.png?rev=1082746
                            [1x] => https://ps.w.org/advanced-custom-fields/assets/icon-128x128.png?rev=1082746
                        )

                    [banners] => Array
                        (
                            [2x] => https://ps.w.org/advanced-custom-fields/assets/banner-1544x500.jpg?rev=1729099
                            [1x] => https://ps.w.org/advanced-custom-fields/assets/banner-772x250.jpg?rev=1729102
                        )

                    [banners_rtl] => Array
                        (
                        )

                    [tested] => 5.4.2
                    [requires_php] => 5.4
                    [compatibility] => stdClass Object
                        (
                        )

                )

            [amp/amp.php] => stdClass Object
                (
                    [id] => w.org/plugins/amp
                    [slug] => amp
                    [plugin] => amp/amp.php
                    [new_version] => 1.5.5
                    [url] => https://wordpress.org/plugins/amp/
                    [package] => https://downloads.wordpress.org/plugin/amp.1.5.5.zip
                    [icons] => Array
                        (
                            [2x] => https://ps.w.org/amp/assets/icon-256x256.png?rev=1987390
                            [1x] => https://ps.w.org/amp/assets/icon-128x128.png?rev=1987390
                        )

                    [banners] => Array
                        (
                            [2x] => https://ps.w.org/amp/assets/banner-1544x500.png?rev=1987390
                            [1x] => https://ps.w.org/amp/assets/banner-772x250.png?rev=1987390
                        )

                    [banners_rtl] => Array
                        (
                        )

                    [tested] => 5.4.2
                    [requires_php] => 5.6
                    [compatibility] => stdClass Object
                        (
                        )

                )

            [edit-author-slug/edit-author-slug.php] => stdClass Object
                (
                    [id] => w.org/plugins/edit-author-slug
                    [slug] => edit-author-slug
                    [plugin] => edit-author-slug/edit-author-slug.php
                    [new_version] => 1.7.0
                    [url] => https://wordpress.org/plugins/edit-author-slug/
                    [package] => https://downloads.wordpress.org/plugin/edit-author-slug.1.7.0.zip
                    [icons] => Array
                        (
                            [2x] => https://ps.w.org/edit-author-slug/assets/icon-256x256.png?rev=1582213
                            [1x] => https://ps.w.org/edit-author-slug/assets/icon.svg?rev=1582213
                            [svg] => https://ps.w.org/edit-author-slug/assets/icon.svg?rev=1582213
                        )

                    [banners] => Array
                        (
                            [2x] => https://ps.w.org/edit-author-slug/assets/banner-1544x500.png?rev=1581739
                            [1x] => https://ps.w.org/edit-author-slug/assets/banner-772x250.png?rev=1582213
                        )

                    [banners_rtl] => Array
                        (
                        )

                    [tested] => 5.4.2
                    [requires_php] => 5.6.20
                    [compatibility] => stdClass Object
                        (
                        )

                )

            [essential-addons-for-elementor-lite/essential_adons_elementor.php] => stdClass Object
                (
                    [id] => w.org/plugins/essential-addons-for-elementor-lite
                    [slug] => essential-addons-for-elementor-lite
                    [plugin] => essential-addons-for-elementor-lite/essential_adons_elementor.php
                    [new_version] => 4.0.4
                    [url] => https://wordpress.org/plugins/essential-addons-for-elementor-lite/
                    [package] => https://downloads.wordpress.org/plugin/essential-addons-for-elementor-lite.4.0.4.zip
                    [icons] => Array
                        (
                            [2x] => https://ps.w.org/essential-addons-for-elementor-lite/assets/icon-256x256.png?rev=2285897
                            [1x] => https://ps.w.org/essential-addons-for-elementor-lite/assets/icon-128x128.png?rev=2285897
                        )

                    [banners] => Array
                        (
                            [2x] => https://ps.w.org/essential-addons-for-elementor-lite/assets/banner-1544x500.gif?rev=2317134
                            [1x] => https://ps.w.org/essential-addons-for-elementor-lite/assets/banner-772x250.gif?rev=2317134
                        )

                    [banners_rtl] => Array
                        (
                        )

                    [tested] => 5.4.2
                    [requires_php] => 5.4
                    [compatibility] => stdClass Object
                        (
                        )

                )

            [gravity-forms-google-analytics-event-tracking/gravity-forms-event-tracking.php] => stdClass Object
                (
                    [id] => w.org/plugins/gravity-forms-google-analytics-event-tracking
                    [slug] => gravity-forms-google-analytics-event-tracking
                    [plugin] => gravity-forms-google-analytics-event-tracking/gravity-forms-event-tracking.php
                    [new_version] => 2.3.10
                    [url] => https://wordpress.org/plugins/gravity-forms-google-analytics-event-tracking/
                    [package] => https://downloads.wordpress.org/plugin/gravity-forms-google-analytics-event-tracking.2.3.10.zip
                    [icons] => Array
                        (
                            [2x] => https://ps.w.org/gravity-forms-google-analytics-event-tracking/assets/icon-256x256.png?rev=1777199
                            [1x] => https://ps.w.org/gravity-forms-google-analytics-event-tracking/assets/icon-128x128.png?rev=1777199
                        )

                    [banners] => Array
                        (
                            [2x] => https://ps.w.org/gravity-forms-google-analytics-event-tracking/assets/banner-1544x500.png?rev=1777199
                            [1x] => https://ps.w.org/gravity-forms-google-analytics-event-tracking/assets/banner-772x250.png?rev=1777199
                        )

                    [banners_rtl] => Array
                        (
                        )

                    [upgrade_notice] => <p>Prevent empty values to be sent to analytics when using partial entries.</p>
                    [tested] => 5.4.2
                    [requires_php] => 5.6
                    [compatibility] => stdClass Object
                        (
                        )

                )

I try this but get error of both slug and new version being an undefined index.

foreach($plugins_us as $item) {
    echo $item['slug'];
    echo $item['new_version'];

    // to know what's in $item
    echo '<pre>'; var_dump($item);
}

The objects from which you're attempting to get those values isn't an array but rather a generic object which is why you cannot access the value using its index. Try instead accessing them as attributes on the class:

echo $item->slug;
echo $item->new_version;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM