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.
Related
This question already has answers here:
How to delete a key and return the value from a PHP array?
(4 answers)
Closed 4 years ago.
Is there a native PHP function for removing an element from an associative array, and returning the value?
Like unset() but with a return value, or array_shift() where you can specify the index to shift?
$element = unset($array['index']);
$element = array_shift($array, 'index');
I know it's easy to do, I'm just curious if there's an elegant one-liner for doing this.
Looking quickly at the official PHP documentation, in the current version (7.2) doesn't have a function that removes and returns an element by the key.
But as you mentioned there are several ways to solve this problem. As you can see at: https://stackoverflow.com/a/10898827/4214312
This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass
This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page
This question already has an answer here:
Populating associative arrays
(1 answer)
Closed 7 years ago.
Running into an issue with a simple PHP script and I can't seem to figure it out. States its on line 3 and I don't see it. Need a fresh set of eyes please.
<?php
$numArr = [];
for ($i=0;$i<5;$i++) {
array_push($numArr,mt_rand());
}
echo min($numArr);
?>
It's likely how you're initializing your array. The [] syntax is only available from PHP 5.4 and above.
From the PHP manual:
As of PHP 5.4 you can also use the short array syntax, which replaces
array() with [].
You can use array () instead.
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];