PHP - nested Arrays
From this global variable:
echo '<pre>';
print_r($wp_registered_sidebars);
echo '</pre>';
I'm getting output like this:
Array
(
[sidebar-1] => Array
(
[name] => Main Sidebar
[id] => sidebar-1
[description] => text
[before_widget] =>
[after_widget] =>
[before_title] =>
[after_title] =>
)
[sidebar-2] => Array
(
[name] => Footer Sidebar 1
[id] => sidebar-2
[des开发者_StackOverflow中文版cription] => text
[before_widget] =>
[after_widget] =>
[before_title] =>
[after_title] =>
)
[sidebar-3] => Array
(
[name] => Footer Sidebar 2
[id] => sidebar-3
[description] => text
[before_widget] =>
[after_widget] =>
[before_title] =>
[after_title] =>
)
)
I never know how many sidebars there are and how they are named.
Is there a way of:
- listing all sidebrs IDs only - sidebar-1, sidebar-2, sidebar-3,
- listing all sidebar names only - Main Sidebar, Footer Sidebar 1, Footer Sidebar 2,
Thank you!
$resultSlideBars = array();
foreach($wp_registered_sidebars as $slidebars)
{
$resultSlideBars[$slidebars['id']] = $slidebars['name']; // will store array as array(id => name
}
// ids
echo implode(' ', array_keys($resultSlideBars));
echo implode(' ', $resultSlideBars); // echo names
or
foreach($wp_registered_sidebars as $slidebars)
{
echo $slidebars['id']; // echo id
echo $slidebars['name']; // echo name
}
foreach(array_keys($wp_registered_sidebars) as $sidebarID) {
echo $sidebarID;
}
foreach($wp_registered_sidebars as $sidebar) {
echo $sidebar['name'];
}
$sidebarIds = array_keys($wp_registered_sidebars);
$sideBarNames = array();
foreach($wp_registered_sidebars as $row)
{
$sideBarNames[] = $row['name'];
}
foreach ($wp_registered_sidebars as $key => $sidebars) { echo "sidebar id" . $key; echo "sidebar name " . $sidebar['name']; }
精彩评论