PHP Array Get Previous Element from Current Key
Now, let's see post of php array get previous element from key. This post will give you simple example of how to get previous element of array php . This tutorial will give you simple example of php get previous key in array. you will learn php get previous key value from current key in array.
Let's see very simple example of how to get previous value from current key in php array with output:
index.php
<?php
$myArray = ["PHP", "Laravel", "Angular", "React", "Vue"];
foreach($myArray as $key => $value){
echo "<br/> Current: ". $value;
echo " | Previous: ". getPrevValue($key, $myArray);
}
function getPrevValue($key, $hash = array())
{
$keys = array_keys($hash);
$found_index = array_search($key, $keys);
if ($found_index === false || $found_index === 0)
return false;
return $hash[$keys[$found_index-1]];
}
?>
Output
Current: PHP | Previous:
Current: Laravel | Previous: PHP
Current: Angular | Previous: Laravel
Current: React | Previous: Angular
Current: Vue | Previous: React
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- PHP Remove Element from Array by Value Example
- Laravel Eloquent Select Single Column to Array Example
- How to Remove Specific Element by Value from Array in PHP?
- How to Remove undefined Value from Array in JQuery?
- How to Get Maximum Key Value of Array in PHP?
- How to Remove Empty Values from Array in PHP?
- How to Add Prefix in Each Key of PHP Array?
- How to Get Minimum Key Value of Array in PHP?
- How to Remove Null Values from Array in PHP?
- How can Make an Array from the Values of Another Array's Key Value?