I have this example array/object:
//$monday array values
Array (
[menu_item1] =>
[menu_item2] =>
Array (
[0] =>
WP_Post Object (
[ID] => 530
[post_content] => Food selection 2
[post_title] => Food 2 ) )
I'm beginner in WP_Post Objects
. How can I get the menu_item1
ID
(if it has value) and menu_item2
ID
value (if it has too)?
The documentation isn't helped me.
I'm sure it's something like but it doesn't work:
foreach ($monday as $key => $value) {
if ($value) {
$myid = $value->ID;
}
}
From comments( @Erwin ).
foreach ($monday as $key => $value) {
if ($value) {
$myid = $value[0]->ID; // this way you can access to object and get id
}
}
Checking:
foreach ($monday as $key => $value) {
if (!empty($value)) { //checking all values to not be empty
if(!empty($value[0])){ //checking, if exists something where you have id
$myid = $value[0]->ID;
}
}
}
Also, you can double check in second statement to get only objects:
foreach ($monday as $key => $value) {
if (!empty($value)) {
if(!empty($value[0]) && is_object($value[0])){
$myid = $value[0]->ID;
}
}
}
Also, remember. If sort of your array will changed, then the code above will not gave you result as expected.