PHP Array: Exercise-50 with Solution
Write a PHP script to get the last value of an array without affecting the pointer.
Sample Solution:
PHP Code:
Sample Output:
Green Black Green
Flowchart:
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP script to get an array containing all the entries of an array which have the keys that are
present in another array.
Next: Write a PHP program to filter out some array elements with certain key-names.
PHP: Tips of the Day
PHP: What is the difference between bindParam and bindValue?
Unlike PDOStatement::bindValue[], the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute[] is called.
And execute
call PDOStatement::bindParam[] to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
Example:
$value = 'foo'; $s = $dbh->prepare['SELECT name FROM bar WHERE baz = :baz']; $s->bindParam[':baz', $value]; // use bindParam to bind the variable $value = 'foobarbaz'; $s->execute[]; // executed with WHERE baz = 'foobarbaz'
or
$value = 'foo'; $s = $dbh->prepare['SELECT name FROM bar WHERE baz = :baz']; $s->bindValue[':baz', $value]; // use bindValue to bind the variable's value $value = 'foobarbaz'; $s->execute[]; // executed with WHERE baz = 'foo'
Ref : //bit.ly/2ZU0eys
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
We are given an array with key-value pair, and we need to find the last value of array without affecting the array pointer.
Examples:
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:
Output:
Bangalore Array [ [c1] => Delhi [c2] => Kolkata [c3] => Mumbai [c4] => Bangalore ]
The many answers in this thread present us with many different options. To be able to choose from them I needed to understand their behavior and performance. In this answer I will share my findings with you, benchmarked against PHP versions 5.6.38
, 7.2.10
and 7.3.0RC1
[expected Dec 13 2018].
The options [s] I will test are:
- option .1.
$x = array_values[array_slice[$array, -1]][0];
[as suggested by rolacja] - option .2.
$x = array_slice[$array, -1][0];
[as suggested by Stoutie] - option
.3.
$x = array_pop[[array_slice[$array, -1]]];
[as suggested by rolacja] - option .4.
$x = array_pop[[array_slice[$array, -1, 1]]];
[as suggested by Westy92] - option .5.
$x = end[$array]; reset[$array];
[as suggested by Iznogood] - option .6.
$x = end[[array_values[$array]]];
[as suggested by TecBrat] - option .7.
$x = $array[count[$array]-1];
[as suggested by Mirko Pagliai] - option .8.
$keys = array_keys[$array]; $x = $array[$keys[count[$keys]-1]];
[as suggested by thrau] - option .9.
$x = $array[] = array_pop[$array];
[as suggested by user2782001] - option 10.
$x = $array[array_key_last[$array]];
[as suggested by Quasimodo's clone ; available per PHP 7.3]
[functions mentioned: array_key_last , array_keys , array_pop , array_slice , array_values , count , end , reset]
The test inputs [s] to combine with:
- null =
$array = null;
- empty =
$array = [];
- last_null =
$array = ["a","b","c",null];
- auto_idx =
$array = ["a","b","c","d"];
- shuffle
=
$array = []; $array[1] = "a"; $array[2] = "b"; $array[0] = "c";
- 100 =
$array = []; for[$i=0;$i