This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 8 years ago.
I was wondering that we have this function in PHP:
mysqli_fetch_row($result);
and this returns a simple array, but using it as an array like
mysqli_fetch_row($result)[0];
to get the first element of the array doesn't work, so you have to create a new variable which will contain the returned array.
Could anyone explain why isn't this working?
Because mysqli_fetch_row is a function that returns a value - it's not an array itself.
Assigning a variable will retrieve the result. Unfortunately that's how it works in earlier versions of PHP.
Gerald's link identifies the functionality it as availble in PHP 5.4, so I guess that you're using an earlier version.
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 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];
This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];
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.