We are given an array with key-value pair, and we need to find the last value of array without affecting the array pointer.
Input : $arr = array('c1' => 'Red', 'c2' => 'Green', 'c3' => 'Blue', 'c4' => 'Black') Output : Black Input : $arr = array('p1' => 'New York', 'p2' => 'Germany', 'p3' => 'England', 'p4' => 'France') Output : France
The above problem can be easily solved using PHP. The idea is to create a copy of the original array and then use the array_pop() inbuilt function, to get the last value of the array. As we are using the array_pop() function on the copy array, so the pointer of the original array remains unchanged.
Built-in function used:
- array_pop(): The function is used to delete or pop the last element of an array.
Below is the implementation of the above approach:
Bangalore Array ( [c1] => Delhi [c2] => Kolkata [c3] => Mumbai [c4] => Bangalore )
- CSS | pointer-events Property
- PHP | echo and print
- p5.js | print() function
- What is the difference between array_merge and array + array in PHP?
- Print PHP Call Stack
- What is the difference between echo, print, and print_r in PHP?
- PHP str_pad to print string patterns
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- PHP program to print an arithmetic progression series using inbuilt functions
- PHP | array() Function
- How to get random value out of an array in PHP?
- PHP append one array to another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.