<p>any idea how if the following is possible in PHP as a single line ?:</p>

<pre><code>&lt;?php
$firstElement = functionThatReturnsAnArray()[0];
</code></pre>

<p>... It doesn't seem to 'take'. I need to do this as a 2-stepper:</p>

<pre><code>&lt;?php
$allElements = functionThatReturnsAnArray();
$firstElement = $allElements[0];
</code></pre>

<p>... just curious - other languages I play with allow things like this, and I'm lazy enoug to miss this in PHP ... any insight appreciated ...</p>

[{'answer_id': 68716, 'author': 'owenmarshall', 'author_id': 9806, 'author_profile': 'https://Stackoverflow.com/users/9806', 'pm_score': 3, 'selected': False, 'text': '<p>Unfortunately, that is not possible with PHP. You have to use two lines to do it.</p>\n'}, {'answer_id': 68719, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': -1, 'selected': False, 'text': '<p>As far as I know this is not possible, I have wanted to do this myself several times.</p>\n'}, {'answer_id': 68745, 'author': 'calebbrown', 'author_id': 7007, 'author_profile': 'https://Stackoverflow.com/users/7007', 'pm_score': 4, 'selected': True, 'text': "<p>Try:</p>\n\n<pre><code>&lt;?php\n$firstElement = reset(functionThatReturnsAnArray());\n</code></pre>\n\n<p>If you're just looking for the first element of the array.</p>\n"}, {'answer_id': 68789, 'author': 'Jack B Nimble', 'author_id': 3800, 'author_profile': 'https://Stackoverflow.com/users/3800', 'pm_score': -1, 'selected': False, 'text': '<p><a href="http://us3.php.net/reset" rel="nofollow noreferrer">http://us3.php.net/reset</a></p>\n\n<p>Only available in php version 5.</p>\n'}, {'answer_id': 68828, 'author': 'Scott Reynen', 'author_id': 10837, 'author_profile': 'https://Stackoverflow.com/users/10837', 'pm_score': 2, 'selected': False, 'text': "<p>list() is useful here. With any but the first array element, you'll need to pad it with useless variables. For example:</p>\n\n<pre><code>list( $firstElement ) = functionThatReturnsAnArray();\nlist( $firstElement , $secondElement ) = functionThatReturnsAnArray();\n</code></pre>\n\n<p>And so on.</p>\n"}, {'answer_id': 68841, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 3, 'selected': False, 'text': "<p>@Scott Reynen</p>\n\n<p>that's not true. This will work:</p>\n\n<pre><code>list(,,$thirdElement) = $myArray;\n</code></pre>\n"}, {'answer_id': 68926, 'author': 'Scott Reynen', 'author_id': 10837, 'author_profile': 'https://Stackoverflow.com/users/10837', 'pm_score': 0, 'selected': False, 'text': '<p>nickf, good to know, thanks. Unfortunately that has readability problems beyond a few commas.</p>\n'}, {'answer_id': 68965, 'author': 'neouser99', 'author_id': 10669, 'author_profile': 'https://Stackoverflow.com/users/10669', 'pm_score': -1, 'selected': False, 'text': "<p>If it's always the first element, you should probably think about having the function return just the first item in the array. If that is the most common case, you could use a little bit of coolness:</p>\n\n<pre><code>function func($first = false) {\n    ...\n    if $first return $array[0];\n    else return $array;\n}\n\n$array = func();\n$item = func(true);\n</code></pre>\n\n<p>My php is slightly rusty, but i'm pretty sure that works.</p>\n\n<p>You can also look at array_shift() and array_pop().</p>\n\n<p>This is probably also possible:</p>\n\n<pre><code>array(func())[0][i];\n</code></pre>\n\n<p>The 0 is for the function.</p>\n"}, {'answer_id': 69303, 'author': 'Garrett Albright', 'author_id': 11023, 'author_profile': 'https://Stackoverflow.com/users/11023', 'pm_score': 3, 'selected': False, 'text': '<p>You <em>can</em> do this in one line! Use <a href="http://us.php.net/array_shift" rel="noreferrer">array_shift()</a>.</p>\n\n<pre><code>&lt;?php\n\necho array_shift(i_return_an_array());\n\nfunction i_return_an_array() {\n    return array(\'foo\', \'bar\', \'baz\');\n}\n</code></pre>\n\n<p>When this is executed, it will echo "<code>foo</code>".</p>\n'}, {'answer_id': 70092, 'author': 'Greg', 'author_id': 1916, 'author_profile': 'https://Stackoverflow.com/users/1916', 'pm_score': 1, 'selected': False, 'text': '<p>Either <code>current($array)</code> or <code>array_shift($array)</code> will work, the former will leave the <em>array</em> intact.</p>\n'}, {'answer_id': 70310, 'author': 'Draco777', 'author_id': 11473, 'author_profile': 'https://Stackoverflow.com/users/11473', 'pm_score': 0, 'selected': False, 'text': '<p>Well, I have found a couple of ways to get what you want without calling another function.</p>\n\n<pre><code>$firstElement = ($t = functionThatReturnsAnArray()) ? $t[0] : false;\n</code></pre>\n\n<p>and for strings you could use </p>\n\n<pre><code>$string = (($t = functionThatReturnsAnArray())==0) . $t[0];\n</code></pre>\n\n<p>.. Interesting problem</p>\n\n<p>Draco</p>\n'}, {'answer_id': 70699, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>You can use <code>array_slice()</code>, like so:</p>\n\n<pre><code>$elementX = array_slice(functionThatReturnsAnArray(), $x, 1);\n</code></pre>\n\n<p>Also noticed that <code>end()</code> is not mentioned. It returns the last element of an array.</p>\n'}, {'answer_id': 72510, 'author': 'Adam Hopkinson', 'author_id': 12280, 'author_profile': 'https://Stackoverflow.com/users/12280', 'pm_score': 0, 'selected': False, 'text': "<p>I think any of the above would require a comment to explain what you're doing, thus becoming two lines. I find it simpler to do:</p>\n\n<pre><code>$element = functionThatReturnsArray();\n$element = $element[0];\n</code></pre>\n\n<p>This way, you're not using an extra variable and it's obvious what you're doing.</p>\n"}, {'answer_id': 72916, 'author': 'deceze', 'author_id': 476, 'author_profile': 'https://Stackoverflow.com/users/476', 'pm_score': 0, 'selected': False, 'text': '<pre><code>$firstItem = current(returnsArray());\n</code></pre>\n'}, {'answer_id': 73923, 'author': 'JW.', 'author_id': 4321, 'author_profile': 'https://Stackoverflow.com/users/4321', 'pm_score': -1, 'selected': False, 'text': "<p>Sometimes I'll change the function, so it can optionally return an element instead of the entire array:</p>\n\n<pre><code>&lt;?php\nfunction functionThatReturnsAnArray($n = NULL) {\n  return ($n === NULL ? $myArray : $myArray[$n]);\n}\n$firstElement = functionThatReturnsAnArray(0);\n</code></pre>\n"}, {'answer_id': 74530, 'author': 'SeanDowney', 'author_id': 5261, 'author_profile': 'https://Stackoverflow.com/users/5261', 'pm_score': 1, 'selected': False, 'text': "<p>I actually use a convenience function i wrote for such purposes:</p>\n\n<pre><code>/**\n * Grabs an element from an array using a key much like array_pop\n */\nfunction array_key_value($array, $key) {\n    if(!empty($array) &amp;&amp; array_key_exists($key, $array)) {\n        return $array[$key];\n    }\n    else {\n        return FALSE;\n    }\n}\n</code></pre>\n\n<p>then you just call it like so:</p>\n\n<pre><code>$result = array_key_value(getMeAnArray(), 'arrayKey');\n</code></pre>\n"}, {'answer_id': 299108, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I am guessing that this is a built-in or library function, since it sounds like you cannot edit it directly.  I recommend creating a wrapper function to give you the output you need:</p>\n\n<pre><code>function functionThatReturnsOneElement( $arg )\n{\n    $result = functionThatReturnsAnArray( $arg );\n    return $result[0];\n}\n$firstElement = functionThatReturnsOneElement();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68711', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']