ItSolutionStuff.com

PHP Array Get Previous Element from Current Key

By Hardik Savani • May 14, 2024
PHP

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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Remove Element from Array by Value Example

Read Now →

Laravel Eloquent Select Single Column to Array Example

Read Now →

How to Remove Specific Element by Value from Array in PHP?

Read Now →

How to Remove undefined Value from Array in JQuery?

Read Now →

How to Get Maximum Key Value of Array in PHP?

Read Now →

How to Remove Empty Values from Array in PHP?

Read Now →

How to Add Prefix in Each Key of PHP Array?

Read Now →

How to Get Minimum Key Value of Array in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →

How can Make an Array from the Values of Another Array's Key Value?

Read Now →