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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With