views:

238

answers:

6

Is there any way to do this in one line?

$arr = $foo->getBarArray();
return $arr[0];

This throws an error:

return $foo->getBarArray()[0];
A: 

Does this work?

 return ($foo->getBarArray())[0];

Otherwise, can you post the getBarArray() function? I don't see why that wouldn't work from what you posted so far.

thedz
No that doesn't work either. Regardless of the function, it throws an "unexpected [" error.
James Skidmore
+6  A: 

There isn't a way to do that unfortunately, although it is in most other programming languages.

If you really wanted to do a one liner, you could make a function called a() and do something like

$test = a(func(), 1); // second parameter is the key.

But other than that, func()[1] is not supported in PHP.

Chacha102
Oh wow, I didn't know that. Do you know why that doesn't work? Shouldn't func() be essentially an array type with the return value, so [1] acts on an array? Or does PHP parse it poorly?
thedz
PHP does not parse it like other languages do, so you have to define it as a variable first.
Chacha102
Is that a limitation of the parser?
thedz
Not really sure.
Chacha102
Ahh that's too bad. Thanks for the tip!
James Skidmore
There is already such function, it's called `array_slice`.
Kuroki Kaze
@Kouroki Kaze: array_slice still returns an array, even if the slice would result in a single value. You could combine it with current, but that's starting to get a bit long for a single line. ;-)
James
+1  A: 

This is too far-fetched, but if you really NEED it to be in one line:


return index0( $foo->getBarArray() );

/* ... */

function index0( $some_array )
{
  return $some_array[0];
}

Petruza
+1  A: 
return array_shift($foo->getBarArray());

http://php.net/array_shift

This is also a duplicate, it's been asked several times.

The Wicked Flea
This is the desired affect, but you can't use that for any other index, like the second or 3rd, which is what he is asking.
Chacha102
Bah... used the wrong 'effect'.
Chacha102
Thanks for the help!
James Skidmore
+4  A: 

As others have mentioned, this isn't possible. PHP's syntax doesn't allow it. However, I do have one suggestion that attacks the problem from the other direction.

If you're in control of the getBarArray method and have access to the PHP Standard Library (installed on many PHP 5.2.X hosts and installed by default with PHP 5.3) you should consider returning an ArrayObject instead of a native PHP array/collection. ArrayObjects have an offetGet method, which can be used to retrieve any index, so your code might look something like

<?php
class Example {
 function getBarArray() {
  $array = new ArrayObject();
  $array[] = 'uno';
  $array->append('dos');
  $array->append('tres');
  return $array;
 }
}

$foo = new Example();
$value = $foo->getBarArray()->offsetGet(2);

And if you ever need a native array/collection, you can always cast the results.

//if you need 
$array = (array) $foo->getBarArray();
Alan Storm
+1, I'll definitely look into that. Thanks Alan!
James Skidmore
+2  A: 

If you just want to return the first item in the array, use the current() function.

return current($foo->getBarArray());

http://us2.php.net/manual/en/function.current.php

AntonioCS