1

In PHP I have the following construct

$a = array(-1 => '-', 0 => '?', 1 => '+')[1];

which gives a syntax error. Is it still possible to do such things in one convenient line avoiding multiple if/else clases or switch/select statements? I was thinking at python where this work fine:

a = {-1:'-', 0:'?', 1:'+'}[1]
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Array dereferencing has been available since PHP 5.4 – John Conde Apr 24 '13 at 13:02
  • Why not just using `print '?'` ? Looks simpler.. :) – hek2mgl Apr 24 '13 at 13:04
  • @JohnConde Nice (news to me) - listed as '[function array dereferencing](http://php.net/manual/en/migration54.new-features.php)' – searlea Apr 24 '13 at 13:04
  • I need this to work in PHP4, and I just was giving an example (of course the '1' is a variable with different possible values 0, -1 and 1).... – Alex Apr 24 '13 at 13:06
  • PHP4?! Are you serious?! – str Apr 24 '13 at 13:07
  • Why are you using `PHP 4` – Baba Apr 24 '13 at 13:15
  • 2
    @Alex Just out of curiosity, why PHP 4? I don't want to judge your motivation, just want to hear from you. – hakre Apr 24 '13 at 13:20
  • I am working in a company on a large (and old!) software system written in PHP4. I am not sure if I can convince the business people to spend a lot of money to completly rewrite this system in a more convenient (and newer) language... – Alex Apr 24 '13 at 14:33
  • @Alex: (IMHO) There's not a massive amount of work [migration to PHP 5](http://www.php.net/manual/en/migration5.incompatible.php). Perhaps you could try running on PHP 5 and running a regression test. There may be no need to rewrite. – searlea Apr 24 '13 at 15:03
  • A PHP4 app having regression tests? That was a good laugh. Let me place my guess: Likely not ;) – hakre Apr 24 '13 at 15:07
  • @hakre I was thinking of a general [regression test](http://en.wikipedia.org/wiki/Regression_test) - i.e. does the software still function if you run it on PHP 5; can you still do all you'd expect to do. Does any behaviour change... It's always been quite common to do this stuff manually, especially in ye olden days. – searlea Apr 24 '13 at 18:29
  • @searlea: Yeah, but first you need to have the tests - manually or not. So for manual tests, you need to have the testsuite where it is written down what to test, how to test it and what qualifies are OK or FAIL. Many old applications are just tested once, then never again - unless something new comes in which will make that new test but nothing else. And then again: never again. – hakre Apr 24 '13 at 18:38

4 Answers4

6

It works in PHP but only 5.5.0alpha1 - 5.5.0beta2 you should use variables for now until a stable version is released.

$array =  array(-1 => '-', 0 => '?', 1 => '+');
print $array[1];

Another Intresting thing is that PHP Support Function Array Dereferencing in PHP 5.4 which means just wrapping your array in a function would make it work

function __($_) {
    return $_;
}

print __(array(-1 => '-', 0 => '?', 1 => '+'))[1];
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Ok I see. I will use your suggestion, as I have to make this work anyway in PHP4... – Alex Apr 24 '13 at 13:05
  • Apart from the fact that this approach is terribly inefficient of course. `$x=$array[1];` takes about **40 nSec** on my laptop and `$x=__(array(-1 => '-', 0 => '?', 1 => '+'))[1]` takes **526 nSec** (based on a 10M iteration timer). OK the odd half mSec may not be significant, but if you are using this as a general approach, it could add up. The issue here is that subroutine calls in PHP are a lot more costly than inline array access. – TerryE Jul 03 '13 at 13:53
1

You could create a helper function so you can do it in one line.

function array_get($array, $key)
{
    return $array[$key]
}

print array_get(array(-1 => '-', 0 => '?', 1 => '+'), 1);
nvanesch
  • 2,540
  • 14
  • 25
0

Regardless which PHP version, if you're after that, you should have something in your function set:

function deref($subject) {
    return $subject;
}

function deref_array($array, $key) {
    return $array[$key];
}

This pair of very rudimentary functions allows you to tell the PHP parser most often what you need and mean:

$a = deref_array(array(-1 => '-', 0 => '?', 1 => '+'), 1);

In your concrete case you only need the second function, but the first one often is useful, too.

hakre
  • 193,403
  • 52
  • 435
  • 836
-1

The one-line way is:

$a = array_pop(array_slice(array(-1 => '-', 0 => '?', 1 => '+'), 1, 1));

Or in the general case:

$x = array_pop(array_slice(foo(), $offset, 1));

Which is of course horrible.

searlea
  • 8,173
  • 4
  • 34
  • 37
  • 1
    `array_pop` requires a variable - you don't use it here. Wrap it in parenthesis to fool PHP here, albeit that is probably not stable. – hakre Apr 24 '13 at 13:25
  • See as well: [Parentheses altering semantics of function call result](http://stackoverflow.com/q/6726589/367456) – hakre Apr 24 '13 at 13:33
  • @hakre: I tested it on PHP 5.3 and it seems to work just fine, no variable reference required. – searlea Apr 24 '13 at 14:59
  • 1
    @sealea: That is because you don't have notices enabled in your development / test environment - which you *should* have - see http://eval.in/private/906625ae413256 – hakre Apr 24 '13 at 15:05