tags:

views:

185

answers:

8

Can this be done in 1 line with PHP?

Would be awesome if it could:

$out = array("foo","bar");
echo $out[0];

Something such as:

echo array("foo","bar")[0];

Unfortunately that's not possible. Would it be possible like this?

So I can do this for example in 1 line:

echo array(rand(1,100), rand(1000,2000))[rand(0,1)];

So let's say I have this code:

 switch($r){
      case 1: $ext = "com"; break;
      case 2: $ext = "nl"; break;
      case 3: $ext = "co.uk"; break;
      case 4: $ext = "de"; break;
      case 5: $ext = "fr"; break;
 }

That would be much more simplified to do it like this:

$ext = array("com","nl","co.uk","de","fr")[rand(1,5)];
+1  A: 

Yes, list a PHP language construct that allows the syntax below.

list( $first_list_element ) = array( "foo", ..... );

EDIT:

Still Yes, Missed the echo. Reset will return the first array item, which might not always be index 0, but if you create an array normally it will.

echo reset( array( "foo",... ) );

EDIT AGAIN:

After you updated your question I see that you want something that PHP just can't do well. I personally think it's a syntax design error of the PHP language/interpreter. If you just mean one-line you could do.

$array = array( .... ); echo $array[0];
Kendall Hopkins
But, he wants to print out the first item (using a simple line), not just set it to a variable.
Cristian
I think current() always returns the first one. Which is not what I want. I want to pick for example a random from the array I just initiated.
Angelo
A: 

I think your example may not be the best. The real syntax limitation here is that one can not immediately perform array access on the returned value of a function call, as in,

do_something_with(explode(',', $str)[0]);

And you pretty much can't get around it. Assign to a variable, then access. It's a silly limitation of PHP, but it's there.

You can technically do,

function array_access($array, $i) {
    return $array[$i];
}
do_something_with(array_access(explode(',', $str), 0));

But please don't. It's even uglier than an extra variable asignment.

(Given the edit to the question, this no longer a sufficient answer. However, I will leave it up for reference.)

Matchu
Damn those limitations.
Angelo
This would be exactly what I want, but ofcourse like you said; it's kind of ugly code.
Angelo
+1  A: 

You can use a shorthand form to keep things on one line and avoid creating an array that will never be used again.

echo rand(0,1) ? rand(1,100) : rand(1000,2000);
thetaiko
Will this be possible? echo rand(0,2) ? rand(1,100), rand(1000,2000), rand(5000,6000);
Angelo
`echo rand(0,1) ? rand(1,100) : rand(0,1) ? rand(1000,2000) : rand(5000,6000);`
thetaiko
@Angelo: Of course, if you want to make it more complicated you should write a function. Keeping everything on one line doesn't always make for readable and efficient code.
thetaiko
@thetaiko: Look out! You have twice as much chance getting a 1-100 as you do 1000-2000 or 5000-6000.
too much php
Ah I already thought... That's a little confusing though.
Angelo
@too much php: indeed you are correct - I simply extended my original answer without thinking too much about it.
thetaiko
+4  A: 

Why not check out the array functions on the PHP site?

Well, if you're picking a random element from the array, you can use array_rand().

$ext = array_rand(array_flip(array("com","nl","co.uk","de","fr")));
animuson
Why array_flip?
Angelo
@Angelo: Read Jonathon's explanation above.
animuson
Aha, I see! Thanks for your answer!
Angelo
+2  A: 
echo array_rand(array_flip(array('foo', 'bar')));

array flip takes an array and swaps the keys with the values and vice versa. array_rand pulls a random element from that array.

Jonathan Kuhn
@webbiedave: That's why you use array_flip to switch keys and values... Then you can reference the array as `$arr[$thatKey]`.
animuson
A: 

print $a[ array_rand($a = array('com','nl','co.uk','de','fr')) ];

Poe
A: 

Like @Matchu's answer, this addresses the more general case, ie you have an array value that came from somewhere, be it a function call, an instantiated array, whatever. Since the return value from an arbitrary function is the least specific case, I'll use a call to some_function() in this example.

$first_element = current(array_slice(some_function(), 0, 1));

So you could randomize it with

$random_element = current(array_slice(some_function(), rand(0,1), 1));

but in that case (as in many in php) there is a more specialized function for that; see @animuson's answer.

edited

changed array_shift() call to current() call: this is more efficient, because array_shift() will modify the intermediate array returned by array_slice().

intuited
A: 

This is being discussed in the internals mailing list right now. You might want to check it: http://marc.info/?l=php-internals&m=127595613412885&w=2

Artefacto