0

配列を返す関数/メソッドがあるとします。それを ArrayReturner() と呼びましょう。しかし、最初の要素 [0] だけが必要です。今、私は次のようなことをしています...

$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];

一時的な $arrayReturned 配列を必要とせずに 1 行でそれを行う方法はありますか?

4

2 に答える 2

2

試す:

$arrayReturned = reset(ArrayReturner());
于 2013-10-22T00:45:15.030 に答える
2

Depends on PHP's version you use.

If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.

If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:

$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
于 2013-10-22T00:54:44.177 に答える