How to Get Last Key in an Array

How to get last key in an array?

A solution would be to use a combination of end and key (quoting) :

  • end() advances array 's internal pointer to the last element, and returns its value.
  • key() returns the index element of the current array position.

So, a portion of code such as this one should do the trick :

$array = array(
'first' => 123,
'second' => 456,
'last' => 789,
);

end($array); // move the internal pointer to the end of the array
$key = key($array); // fetches the key of the element pointed to by the internal pointer

var_dump($key);

Will output :

string 'last' (length=4)

i.e. the key of the last element of my array.

After this has been done the array's internal pointer will be at the end of the array. As pointed out in the comments, you may want to run reset() on the array to bring the pointer back to the beginning of the array.

How to get last array index?


Get the last array index

Simply invert the array, then use end

echo end(array_keys($s));

Get all contents of the last array index

Simply use end through an iteration

foreach($appointments as $app) {
echo end($app) . PHP_EOL;
}

Get only the last element from the sub-array (only output 13)

Simply grab the last sub-array and put it through end

echo end($appointments[ count($appointments) - 1 ]);

And if you want to just get id_services as you can't guarantee this key will always be last, simply reference it as follows;

echo $appointments[ count($appointments) - 1 ]['id_services'];

Get key of the last element in an array


end($array);
echo key($array)

This should return the key of the last element.

Get last key-value pair in PHP array

try to use

end($array);

PHP - Get first and last key and value from array

Separate out keys and values in separate arrays, and extract first and last from them:

// Get all the keys in the array
$all_keys = array_keys($_SERVER);

// Get all the values in the array
$all_values = array_values($_SERVER);

// first key and value
$first_key = array_shift($all_keys);
$first_value = array_shift($all_values);

// last key and value (we dont care about the pointer for the temp created arrays)
$last_key = end($all_keys);
$last_value = end($all_values);

/* you can use reset function after end function call
if you worry about the pointer */

how to get last key in key value pair object

Objects are not arrays, so the keys are not ordered by default. If you apply your own ordering (e.g., sort) then you could determine what's last, but you would have to define the rules of the sort (e.g., ascii-alphabetical).

If order is not important you can do:

Object.keys(valuePair).pop()

Get last element inside new array with key value from Object?

One way is to use Object.assign

newData.push(Object.assign({class:key}, value[value.length -1]))

But, more a modern method, using spread in object literals syntax makes it (I think) easier to read

newData.push({class:key, ...value[value.length - 1]});





let data = {"classA":[{"date":"01-01","present":49,"absent":14},{"date":"02-01","present":39,"absent":24},{"date":"03-01","present":35,"absent":28}],"classB":[{"date":"01-01","present":49,"absent":14},{"date":"02-01","present":39,"absent":24},{"date":"03-01","present":35,"absent":28}],"classC":[{"date":"01-01","present":49,"absent":14},{"date":"02-01","present":39,"absent":24},{"date":"03-01","present":35,"absent":28}]}


let newData = [];


for (let [key, value] of Object.entries(data)) {

newData.push({class:key, ...value[value.length - 1]});

}


console.log(newData);

How to get a last key of an associative array (dictionary)

While Tom Fenech is absolutely right saying
The keys are unordered, so the concept of a "last key" doesn't really make sense, you can avoid the error by changing the line with eval to

keys2=( "${!addict[@]}" )

and see what you get. It may also be illuminating to look at declare -p addict. In order to get some key (first key that is returned from unordered keys list, not first key that was declared) you can do:

some_key="${keys2[0]}"

This way you could, for example, unset A[$some_key] and iterate over keys in such manner by picking first returned key each time.

Example:

$ declare -A A=( [a]=x [b]=y [c]=z )
$ echo "${!A[@]}"
c b a

$ keys=( "${!A[@]}" )
$ echo "${keys[0]}"
c
# you see that c was returned instead of a
# (on your computer order could be different)

$ unset A[$some_key]
$ echo "${!A[@]}"
b a

$ declare -p A
declare -A A=([b]="y" [a]="x" )

Further reading: GNU Bash - Arrays.

How to get the last key of object which has a value

Object properties do have a certain order differing from insertion order that isn't really useful in this case as described by T.J. Crowder. This Stack Overflow question is a good read on the subject, and I recommend reading further than the first answer.

So instead of relying on objects which have no guarantee to order1, use something that does guarantee order. You can use something like a Map, which is similar to an object with key/value pairs, but the pairs are ordered. For example: