从函数返回的PHP数组中获取元素

时间:2022-07-21 01:45:06

I'm not sure if this is possible, but I can't figure out how to do it if it is...

我不确定这是否可行,但如果是......我无法弄清楚如何做到这一点

I want to get a specific element out of an array that is returned by a function, without first passing it into an array... like this...

我想从一个函数返回的数组中获取一个特定的元素,而不是先将它传递给一个数组......就像这样......

$item = getSomeArray()[1];


function getSomeArray(){
     $ret = Array();
     $ret[0] = 0;
     $ret[1] = 100;
     return $ret;
}

What's the correct syntax for this?

这是什么正确的语法?

7 个解决方案

#1


15  

PHP cannot do this yet, it's a well-known limitation. You have every right to be wondering "are you kidding me?". Sorry.

PHP还不能做到这一点,这是一个众所周知的限制。你有权利想知道“你在开玩笑吗?”。抱歉。

If you don't mind generating an E_STRICT warning you can always pull out the first and last elements of an array with reset and end respectively -- this means that you can also pull out any element since array_slice can arrange for the one you want to remain first or last, but that's perhaps taking things too far.

如果您不介意生成E_STRICT警告,您可以随时分别使用reset和end拉出数组的第一个和最后一个元素 - 这意味着您也可以提取任何元素,因为array_slice可以安排您想要的那个元素保持第一或最后,但这可能是太过分了。

But despair not: the (at this time upcoming) PHP 5.4 release will include exactly this feature (under the name array dereferencing).

但绝望不是:(此时即将推出)PHP 5.4版本将包含这个功能(在名称数组解除引用下)。

Update: I just thought of another technique which would work. There's probably good reason that I 've never used this or seen it used, but technically it does the job.

更新:我只想到了另一种可行的技术。可能有充分的理由说我从未使用过它或看过它,但从技术上来说它可以完成这项工作。

// To pull out Nth item of array:
list($item) = array_slice(getSomeArray(), N - 1, 1);

#2


3  

PHP 5.4 has added Array Dereferencing, here's the example from PHP's Array documentation:

PHP 5.4添加了Array Dereferencing,这是PHP的Array文档中的示例:

Example #7 Array dereferencing

示例#7阵列解除引用

function getArray() {
    return ['a', 'b', 'c'];
}

// PHP 5.4
$secondElement = getArray()[0]; // output = a

// Previously
$tmp = getArray();
$secondElement = $tmp[0]; // output = a

#3


2  

The syntax you're referring to is known as function array dereferencing. It's not yet implemented and there's an RFC for it.

您所指的语法称为函数数组解除引用。它尚未实现,并且有一个RFC。

The generally accepted syntax is to assign the return value of the function to an array and access the value from that with the $array[1]syntax.

通常接受的语法是将函数的返回值赋给数组,并使用$ array [1]语法访问该函数的值。

That said, however you could also pass the key you want as an argument to your function.

也就是说,您也可以将您想要的密钥作为参数传递给您的函数。

function getSomeArray( $key = null ) {
  $array = array(
     0 => "cheddar",
     1 => "wensleydale"
  );

  return is_null($key) ? $array : isset( $array[$key] ) ? $array[$key] : false;    
}

echo getSomeArray(0); // only key 0
echo getSomeArray(); // entire array

#4


2  

Yes, php can't do that. Bat you can use ArrayObect, like so:

是的,php无法做到这一点。蝙蝠你可以使用ArrayObect,如下所示:

$item = getSomeArray()->{1};
// Credits for curly braces for Bracketworks    

function getSomeArray(){
     $ret = Array();
     $ret[0] = 0;
     $ret[1] = 100;
     return new ArrayObject($ret, ArrayObject::ARRAY_AS_PROPS);
}

Okay, maybe not working with numeric keys, but i'm not sure.

好吧,也许不使用数字键,但我不确定。

#5


1  

I know this is an old question, but, in your example, you could also use array_pop() to get the last element of the array, (or array_shift() to get the first).

我知道这是一个老问题,但是,在您的示例中,您还可以使用array_pop()来获取数组的最后一个元素,(或者使用array_shift()来获取第一个元素)。

<?php
$item = array_pop(getSomeArray());


function getSomeArray(){
    $ret = Array();
    $ret[0] = 0;
    $ret[1] = 100;
    return $ret;
}

#6


0  

As the others says, there is no direct way to do this, I usually just assign it to a variable like so:

正如其他人所说,没有直接的方法可以做到这一点,我通常只是将它分配给一个变量,如下所示:

$items = getSomeArray();
$item = $items[1];


function getSomeArray(){
    $ret = Array();
    $ret[0] = 0;
    $ret[1] = 100;
    return $ret;
}

#7


0  

I am fairly certain that is not possible to do in PHP. You have to assign the returned array to another array before you can access the elements within or use it directly in some other function that will iterate though the returned array (i.e. var_dump(getSomeArray()) ).

我相当肯定在PHP中是不可能的。您必须先将返回的数组分配给另一个数组,然后才能访问其中的元素,或者直接在其他函数中使用它,这些函数将迭代返回的数组(即var_dump(getSomeArray()))。

#1


15  

PHP cannot do this yet, it's a well-known limitation. You have every right to be wondering "are you kidding me?". Sorry.

PHP还不能做到这一点,这是一个众所周知的限制。你有权利想知道“你在开玩笑吗?”。抱歉。

If you don't mind generating an E_STRICT warning you can always pull out the first and last elements of an array with reset and end respectively -- this means that you can also pull out any element since array_slice can arrange for the one you want to remain first or last, but that's perhaps taking things too far.

如果您不介意生成E_STRICT警告,您可以随时分别使用reset和end拉出数组的第一个和最后一个元素 - 这意味着您也可以提取任何元素,因为array_slice可以安排您想要的那个元素保持第一或最后,但这可能是太过分了。

But despair not: the (at this time upcoming) PHP 5.4 release will include exactly this feature (under the name array dereferencing).

但绝望不是:(此时即将推出)PHP 5.4版本将包含这个功能(在名称数组解除引用下)。

Update: I just thought of another technique which would work. There's probably good reason that I 've never used this or seen it used, but technically it does the job.

更新:我只想到了另一种可行的技术。可能有充分的理由说我从未使用过它或看过它,但从技术上来说它可以完成这项工作。

// To pull out Nth item of array:
list($item) = array_slice(getSomeArray(), N - 1, 1);

#2


3  

PHP 5.4 has added Array Dereferencing, here's the example from PHP's Array documentation:

PHP 5.4添加了Array Dereferencing,这是PHP的Array文档中的示例:

Example #7 Array dereferencing

示例#7阵列解除引用

function getArray() {
    return ['a', 'b', 'c'];
}

// PHP 5.4
$secondElement = getArray()[0]; // output = a

// Previously
$tmp = getArray();
$secondElement = $tmp[0]; // output = a

#3


2  

The syntax you're referring to is known as function array dereferencing. It's not yet implemented and there's an RFC for it.

您所指的语法称为函数数组解除引用。它尚未实现,并且有一个RFC。

The generally accepted syntax is to assign the return value of the function to an array and access the value from that with the $array[1]syntax.

通常接受的语法是将函数的返回值赋给数组,并使用$ array [1]语法访问该函数的值。

That said, however you could also pass the key you want as an argument to your function.

也就是说,您也可以将您想要的密钥作为参数传递给您的函数。

function getSomeArray( $key = null ) {
  $array = array(
     0 => "cheddar",
     1 => "wensleydale"
  );

  return is_null($key) ? $array : isset( $array[$key] ) ? $array[$key] : false;    
}

echo getSomeArray(0); // only key 0
echo getSomeArray(); // entire array

#4


2  

Yes, php can't do that. Bat you can use ArrayObect, like so:

是的,php无法做到这一点。蝙蝠你可以使用ArrayObect,如下所示:

$item = getSomeArray()->{1};
// Credits for curly braces for Bracketworks    

function getSomeArray(){
     $ret = Array();
     $ret[0] = 0;
     $ret[1] = 100;
     return new ArrayObject($ret, ArrayObject::ARRAY_AS_PROPS);
}

Okay, maybe not working with numeric keys, but i'm not sure.

好吧,也许不使用数字键,但我不确定。

#5


1  

I know this is an old question, but, in your example, you could also use array_pop() to get the last element of the array, (or array_shift() to get the first).

我知道这是一个老问题,但是,在您的示例中,您还可以使用array_pop()来获取数组的最后一个元素,(或者使用array_shift()来获取第一个元素)。

<?php
$item = array_pop(getSomeArray());


function getSomeArray(){
    $ret = Array();
    $ret[0] = 0;
    $ret[1] = 100;
    return $ret;
}

#6


0  

As the others says, there is no direct way to do this, I usually just assign it to a variable like so:

正如其他人所说,没有直接的方法可以做到这一点,我通常只是将它分配给一个变量,如下所示:

$items = getSomeArray();
$item = $items[1];


function getSomeArray(){
    $ret = Array();
    $ret[0] = 0;
    $ret[1] = 100;
    return $ret;
}

#7


0  

I am fairly certain that is not possible to do in PHP. You have to assign the returned array to another array before you can access the elements within or use it directly in some other function that will iterate though the returned array (i.e. var_dump(getSomeArray()) ).

我相当肯定在PHP中是不可能的。您必须先将返回的数组分配给另一个数组,然后才能访问其中的元素,或者直接在其他函数中使用它,这些函数将迭代返回的数组(即var_dump(getSomeArray()))。