перебирать несериализованный массив параметров wordpress в php - PullRequest
0 голосов
/ 11 июля 2020

Я пытаюсь в конечном итоге получить ярлык плагина wordpress и значения новой версии из этого массива объектов. Я провел много исследований и пробовал много примеров, но ни один из них не показывает этот формат массива. Меня смущает структура индекса. Помощь приветствуется.

Вот фрагмент структуры массива из несериализованных данных wordpress.

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
                        (
                        )

                )

Я пробую это, но получаю сообщение об ошибке, что и slug, и новая версия являются неопределенным индексом.

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

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

1 Ответ

0 голосов
/ 11 июля 2020

Объекты, из которых вы пытаетесь получить эти значения, не являются массивом, а скорее общим c объектом, поэтому вы не можете получить доступ к значению, используя его индекс. Вместо этого попробуйте получить к ним доступ как к атрибутам класса:

echo $item->slug;
echo $item->new_version;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...