<p>In php, I often need to map a variable using an array ... but I can not seem to be able to do this in a one liner. c.f. example:</p>

<pre><code>// the following results in an error:
echo array('a','b','c')[$key];

// this works, using an unnecessary variable:
$variable = array('a','b','c');
echo $variable[$key];
</code></pre>

<p>This is a minor problem, but it keeps bugging every once in a while ... I don't like the fact, that I use a variable for nothing ;)</p>

[{'answer_id': 13113, 'author': 'onnodb', 'author_id': 1037, 'author_profile': 'https://Stackoverflow.com/users/1037', 'pm_score': 5, 'selected': True, 'text': "<p>I wouldn't bother about that extra variable, really. If you want, though, you could also remove it from memory after you've used it:</p>\n\n<pre><code>$variable = array('a','b','c');\necho $variable[$key];\nunset($variable);\n</code></pre>\n\n<p>Or, you could write a small function:</p>\n\n<pre><code>function indexonce(&amp;$ar, $index) {\n  return $ar[$index];\n}\n</code></pre>\n\n<p>and call this with:</p>\n\n<pre><code>$something = indexonce(array('a', 'b', 'c'), 2);\n</code></pre>\n\n<p>The array should be destroyed automatically now.</p>\n"}, {'answer_id': 31942, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 5, 'selected': False, 'text': "<p>The technical answer is that the <strong><em>Grammar</em></strong> of the PHP language only allows subscript notation on the end of <strong>variable expressions</strong> and not <strong>expressions</strong> in general, which is how it works in most other languages. I've always viewed it as a deficiency in the language, because it is possible to have a grammar that resolves subscripts against any expression unambiguously. It could be the case, however, that they're using an inflexible parser generator or they simply don't want to break some sort of backwards compatibility.</p>\n\n<p>Here are a couple more examples of invalid subscripts on valid expressions:</p>\n\n<pre><code>$x = array(1,2,3);\nprint ($x)[1]; //illegal, on a parenthetical expression, not a variable exp.\n\nfunction ret($foo) { return $foo; }\necho ret($x)[1]; // illegal, on a call expression, not a variable exp.\n</code></pre>\n"}, {'answer_id': 1270349, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Or something like this, if you need the array value in a variable</p>\n\n<pre><code>$variable = array('a','b','c');\n$variable = $variable[$key];\n</code></pre>\n"}, {'answer_id': 2912626, 'author': 'John Smith', 'author_id': 350866, 'author_profile': 'https://Stackoverflow.com/users/350866', 'pm_score': 1, 'selected': False, 'text': '<p>actually, there is an elegant solution:) The following will assign the 3rd element of the array returned by myfunc to $myvar:</p>\n\n<pre><code>$myvar = array_shift(array_splice(myfunc(),2));\n</code></pre>\n'}, {'answer_id': 5040293, 'author': 'tjma2001', 'author_id': 622961, 'author_profile': 'https://Stackoverflow.com/users/622961', 'pm_score': 2, 'selected': False, 'text': '<pre><code>function doSomething()\n{\n    return $somearray;\n}\n\necho doSomething()-&gt;get(1)-&gt;getOtherPropertyIfThisIsAnObject();\n</code></pre>\n'}, {'answer_id': 7888561, 'author': 'Mbrevda', 'author_id': 747749, 'author_profile': 'https://Stackoverflow.com/users/747749', 'pm_score': 5, 'selected': False, 'text': '<p>This is called array dereferencing. It has been added in php 5.4.\n<a href="http://www.php.net/releases/NEWS_5_4_0_alpha1.txt">http://www.php.net/releases/NEWS_5_4_0_alpha1.txt</a></p>\n\n<p><em>update[2012-11-25]:</em> as of PHP 5.5, dereferencing has been added to contants/strings as well as arrays</p>\n'}, {'answer_id': 8613413, 'author': 'Uneebe', 'author_id': 283534, 'author_profile': 'https://Stackoverflow.com/users/283534', 'pm_score': 3, 'selected': False, 'text': '<p>This might not be directly related.. But I came to this post finding solution to this specific problem.</p>\n\n<p>I got a result from a function in the following form.</p>\n\n<pre><code>Array\n(\n    [School] =&gt; Array\n            (\n                [parent_id] =&gt; 9ce8e78a-f4cc-ff64-8de0-4d9c1819a56a\n            )\n)\n</code></pre>\n\n<p>what i wanted was the parent_id value "9ce8e78a-f4cc-ff64-8de0-4d9c1819a56a".\nI used the function like this and got it. </p>\n\n<pre><code>array_pop( array_pop( the_function_which_returned_the_above_array() ) )\n</code></pre>\n\n<p>So, It was done in one line :)\nHope It would be helpful to somebody.</p>\n'}, {'answer_id': 35601314, 'author': 'Dan Jay', 'author_id': 2035600, 'author_profile': 'https://Stackoverflow.com/users/2035600', 'pm_score': 0, 'selected': False, 'text': '<p>There are several <em>oneliners</em> you could come up with, using php <strong>array_*</strong> functions. But I assure you that doing so it is total redundant comparing what you want to achieve.   </p>\n\n<p>Example you can use something like following, but it is not an elegant solution and I\'m not sure about the performance of this;</p>\n\n<pre><code>   array_pop ( array_filter( array_returning_func(), function($key){    return $key=="array_index_you_want"? TRUE:FALSE;    },ARRAY_FILTER_USE_KEY ) );\n</code></pre>\n\n<p>if you are using a php framework and you are stuck with an older version of php, most frameworks has helping libraries.</p>\n\n<p>example: Codeigniter array helpers</p>\n'}, {'answer_id': 51780191, 'author': 'Elementary', 'author_id': 10127060, 'author_profile': 'https://Stackoverflow.com/users/10127060', 'pm_score': 0, 'selected': False, 'text': "<p>though the fact that dereferencing has been added in PHP >=5.4 you could have done it in one line using ternary operator:</p>\n\n<pre><code>echo $var=($var=array(0,1,2,3))?$var[3]:false;\n</code></pre>\n\n<p>this way you don't keep the array only the variable. and you don't need extra functions to do it...If this line is used in a function it will automatically be destroyed at the end but you can also destroyed it yourself as said with <code>unset</code> later in the code if it is not used in a function.</p>\n"}]
['https://Stackoverflow.com/questions/13109', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1532/']