Can this be done in 1 line?

Can this be done in 1 line with PHP?

It would be great if that were possible:

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

Sort of:

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

Unfortunately this is not possible. Is this possible?

Therefore, 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; } 

It would be much easier to do this as follows:

 $ext = array("com","nl","co.uk","de","fr")[rand(1,5)]; 
+4
source share
8 answers

Why not check the array functions in a PHP site ?

Well, if you select a random element from an array, you can use array_rand() .

 $ext = array_rand(array_flip(array("com","nl","co.uk","de","fr"))); 
+4
source
 echo array_rand(array_flip(array('foo', 'bar'))); 

the flip array takes an array and swaps keys with values ​​and vice versa. array_rand extracts a random element from this array.

+2
source

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

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

EDIT:

Still Yes, Missed the echo. Reset will return the first element of the array, which may not always be index 0, but if you create an array , then usually it will.

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

EDIT AGAIN:

After you have updated your question, I see that you want something that PHP simply cannot succeed. I personally consider this a design error for the syntax of the PHP language / interpreter. If you just mean one line you could do.

 $array = array( .... ); echo $array[0]; 
+1
source

You can use the short form to keep things on one line and not create an array that will never be used again.

 echo rand(0,1) ? rand(1,100) : rand(1000,2000); 
+1
source

I think your example may not be the best. The real syntax limitation here is that you cannot immediately access the array by the returned value of the function call, as in,

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

And you pretty much can't get around this. Assign a variable, then access. This is a stupid limitation of PHP, but it is.

You can technically do

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

But please don’t. This is even more ugly than adding an extra variable.

(Given the editing of the question, this is no longer a sufficient answer. However, I will leave it for reference.)

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

Like @Matchu's answer, this concerns a more general case, i.e. you have an array value that happened from somewhere, be it a function call, an instance array, whatever. Since the return value from an arbitrary function is the least specific, I will use the call to some_function() in this example.

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

So you can randomize it with

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

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

edited

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

0
source

This is being discussed on the internal mailing list right now. You can check it out: http://marc.info/?l=php-internals&m=127595613412885&w=2

0
source

Source: https://habr.com/ru/post/1312145/


All Articles