This question already has answers here:
How to get the last element of an array without deleting it?
(33 answers)
Closed 8 years ago.
I'd like to get last element of an array in PHP.
I often do this:
$last = $this->array_of_stuff[count($this->array_of_stuff) - 1];
I'm not very happy with that though.
Any way to make the syntax shorter and avoid repeating code?
You can use end function for this case. See the manual
end($array)
You can use end()
<?php
$a = array("a", "b", "c");
echo end($a);
https://eval.in/188679
Related
This question already has answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed 5 years ago.
Given this PHP array:
$options['systems'] = array(1, 2, 3)
How would I append the value 4 to the $systems array within the $options array?
You could use array_push to push additional items like so:
array_push($options['systems'], 4);
Or the shorthand version:
$options['systems'][] = 4;
You can use php array_push function. Like this. array_push($options['systems'],4);
you can read the detail of array_push from below link.array_push manual
This question already has answers here:
Accessing an array element when returning from a function
(3 answers)
Closed 9 years ago.
Say I have a function/method that returns an array, let's call it ArrayReturner(). But I only want the first element, [0]. Right now I'm doing something like...
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
Is there a way to do that in one line without the need for the temporary $arrayReturned array?
Try:
$arrayReturned = reset(ArrayReturner());
Depends on PHP's version you use.
If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.
If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Access array returned by a function in php
Trivial question; is it possible to get the current index of a returning array from a PHP function, like JavaScript can like this:
function returnSomething()
{
return ['one', 'two', 'three'];
}
var two = returnSomething()[1]; // two
I've always wondered if PHP can do this, I've tried ages ago (but it is invalid to do so), and never got to ask here.
PHP can do this starting from 5.4; it's called array dereferencing.
This question already has answers here:
Get random item from array [duplicate]
(4 answers)
Closed 9 years ago.
I have a simple array like this:
$input = array('Line1', 'Line2', 'Line3');
And want to echo one of the values randomly. I've done this before but can't remember how I did it and all the examples of array_rand seem more complex that what I need.
Can any help? Thanks
echo $input[array_rand($input)];
array_rand() returns the key, so we need to plug it back into $input to get the value.
Complex? Are we on the same manual page?
$rand_key = array_rand($input, 1);
You could use shuffle() and then just pick the first element.
shuffle($input);
echo $input[0];
But I would go with the array_rand() method.
array_rand will help you select a random key of an array. From there you can get the value.
$randKey = array_rand($input);
echo $input[$randKey];
Just a single function: array_rand().
echo $input[array_rand($input,1)];