Reputation: 4282
I know that some array functions, such as array_rand, work for objects as long as you put (array) in front of the object. In this case I am trying to get the ID of the
$this->id = end($this->log->r);
this returns all of the elements in the last element. I just want to know what the key of that element is. This is a JSON_decoded object.
Upvotes: 6
Views: 15206
Reputation: 52
You can cast array before apllying end() method on an object :
# $menu_items is an object returned by wp_get_nav_menu_items() wordpress method
$menu_items = wp_get_nav_menu_items('nav_menu');
$last_item = (array) end($menu_items);
print_r($last_item['title']);
Upvotes: -2
Reputation: 4634
end()
sets the pointer to the last property defined in the object and returns its value.
When the pointer is moved, you can call the key()
function to get the property name
<?php
$object = new stdClass();
$object->first_property = 'first value';
$object->second_property = 'second value';
$object->third_property = 'third value';
$object->last_property = 'last value';
// move pointer to end
end($object);
// get the key
$key = key($object);
var_dump($key);
?>
Outputs
string 'last_property' (length=13)
This functionality is identical for arrays How to get last key in an array
Upvotes: 13