John Davidson

wordpress - Looping over a multidimensional array in php

0 comments
Message:


I need to list plugins that have an update available in wordpress. Ideally it would look like:
"plugin name" "current version" "available update version"
e.g. "real-media-library-lite" "4.9.0" "4.11.0"


so far im getting the data i need (apart from the current version) like so:


$update_plugins = get_site_transient( 'update_plugins' );

if ( ! empty( $update_plugins->response ) )
var_dump($update_plugins->response );


which outputs the following:


array(3) {
["real-media-library-lite/index.php"]=> object(stdClass)#8563 (12) {
["id"]=> string(37) "w.org/plugins/real-media-library-lite"
["slug"]=> string(23) "real-media-library-lite"
["plugin"]=> string(33) "real-media-library-lite/index.php"
["new_version"]=> string(6) "4.11.0"
["url"]=> string(54) "https://wordpress.org/plugins/real-media-library-lite/"
["package"]=> string(66) "https://downloads.wordpress.org/plugin/real-media-library-lite.zip"
["icons"]=> array(2) {
["2x"]=> string(76) "https://ps.w.org/real-media-library-lite/assets/icon-256x256.gif?rev=2293211"
["1x"]=> string(76) "https://ps.w.org/real-media-library-lite/assets/icon-128x128.gif?rev=2293211"
}
["banners"]=> array(2) {
["2x"]=> string(79) "https://ps.w.org/real-media-library-lite/assets/banner-1544x500.png?rev=2251436"
["1x"]=> string(78) "https://ps.w.org/real-media-library-lite/assets/banner-772x250.png?rev=2251436"
}
["banners_rtl"]=> array(0) {
}
["tested"]=> string(3) "5.6"
["requires_php"]=> string(5) "7.0.0"
["compatibility"]=> object(stdClass)#8555 (0) {
}
}
["advanced-custom-fields-pro/acf.php"]=> object(stdClass)#8557 (8) {
["slug"]=> string(26) "advanced-custom-fields-pro"
["plugin"]=> string(34) "advanced-custom-fields-pro/acf.php"
["new_version"]=> string(5) "5.9.3"
["url"]=> string(36) "https://www.advancedcustomfields.com"
["tested"]=> string(5) "5.5.3"
["package"]=> string(0) ""
["icons"]=> array(1) {
["default"]=> string(63) "https://ps.w.org/advanced-custom-fields/assets/icon-256x256.png"
}
["banners"]=> array(2) {
["low"]=> string(77) "https://ps.w.org/advanced-custom-fields/assets/banner-772x250.jpg?rev=1729102"
["high"]=> string(78) "https://ps.w.org/advanced-custom-fields/assets/banner-1544x500.jpg?rev=1729099"
}
}
["real-media-library/index.php"]=> object(stdClass)#8560 (7) {
["slug"]=> string(18) "real-media-library"
["new_version"]=> string(6) "4.11.0"
["package"]=> NULL
["id"]=> int(0)
["url"]=> string(47) "https://devowl.io/wordpress-real-media-library/"
["tested"]=> string(3) "5.6"
["plugin"]=> string(28) "real-media-library/index.php"
}
}


what I can't seem to figure out is how to loop over this array and output the "slug" and the "new_version", heck i can't even output the values of the first dimension of the array dynamically.
So far i was only able to get to the data i need like this:


$update_plugins = get_site_transient( 'update_plugins' );
$slug = $update_plugins->response;
print_r($slug["real-media-library-lite/index.php"]->slug);

but i need to output the data dynamically. any help would be highly appreciated.



My Answer:
To loop over a multidimensional array in PHP and list plugins that have an update available in WordPress, you can use a foreach loop. Here's an example code snippet:

php
$plugins = array(
array(
'name' => 'Plugin 1',
'version' => '1.0',
'update_available' => true
),
array(
'name' => 'Plugin 2',
'version' => '2.0',
'update_available' => false
),
array(
'name' => 'Plugin 3',
'version' => '1.5',
'update_available' => true
)
);

foreach ($plugins as $plugin) {
if ($plugin['update_available']) {
echo $plugin['name'] . ' has an update available.
';
}
}


This code will loop over the `$plugins` array and check if the `update_available` key is set to `true`. If it is, it will echo out the name of the plugin with the message "has an update available". You can customize the output message as needed.

Rate this post

4 of 5 based on 1861 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us