Skip to content

Commit

Permalink
array_key_last()
Browse files Browse the repository at this point in the history
fixes #1148
  • Loading branch information
jakubmisek committed Oct 19, 2024
1 parent a9f3066 commit 1c77b3a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Peachpie.Library/Arrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ public static PhpValue array_key_first(PhpArray array)
public static PhpValue array_key_last(PhpArray array)
{
var enumerator = array.GetFastEnumerator();
if (enumerator.MovePrevious())
if (enumerator.MoveLast())
{
return PhpValue.Create(enumerator.CurrentKey);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/arrays/array_key_last_001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

// https://github.com/peachpiecompiler/peachpie/issues/1148

namespace arrays\array_key_last_001;

// Sample associative array
$fruits = [
'apple' => 'green',
'banana' => 'yellow',
'cherry' => 'red',
];

// Using array_key_last() to get the last key of the array
$lastKey = array_key_last($fruits);

echo "The last key is: " . $lastKey . "\n";
echo "The value of the last element is: " . $fruits[$lastKey] . "\n";

// Sample indexed array
$numbers = [10, 20, 30, 40];

// Using array_key_last() to get the last key of the indexed array
$lastIndex = array_key_last($numbers);

echo "The last index is: " . $lastIndex . "\n";
echo "The value of the last element is: " . $numbers[$lastIndex] . "\n";

0 comments on commit 1c77b3a

Please sign in to comment.