在php中访问没有方括号的数组值

时间:2023-02-01 21:42:49

In php how can I access an array's values without using square brackets around the key? My particular problem is that I want to access the elements of an array returned by a function. Say function(args) returns an array. Why is $var = function(args)[0]; yelling at me about the square brackets? Can I do something like $var = function(args).value(0); or am I missing something very basic?

在PHP中如何在不使用键周围的方括号的情况下访问数组的值?我的特殊问题是我想访问函数返回的数组元素。假设函数(args)返回一个数组。为什么$ var = function(args)[0];关于方括号向我大喊大叫?我可以做一些像$ var = function(args).value(0);还是我错过了一些非常基本的东西?

5 个解决方案

#1


10  

As the others have said, you pretty much have to use a temporary variable:

正如其他人所说,你几乎必须使用一个临时变量:

$temp = myFunction();
$value = $temp[0];

But, if know the structure of the array being returned it is possible to avoid the temporary variable.

但是,如果知道返回的数组的结构,则可以避免临时变量。

If you just want the first member:

如果你只想要第一个成员:

$value = reset(myFunction());

If you want the last member:

如果你想要最后一个成员:

$value = end(myFunction());

If you want any one in between:

如果你想要其中任何一个:

// second member
list(, $value) = myFunction();

// third
list(, , $value) = myFunction();

// or if you want more than one:

list(, , $thirdVar, , $fifth) = myFunction();

#2


2  

In PHP, when getting an array as a function result, you unfortunately have to do an extra step:

在PHP中,当将数组作为函数结果获取时,您不得不做一个额外的步骤:

$temp_array = function($args);
$var = $temp_array[0];

For objects, this has been relaxed in PHP 5. You can do:

对于对象,这已经在PHP 5中放宽了。您可以这样做:

$echo function($args)->property;

(provided function returns an object of course.)

(提供的函数当然会返回一个对象。)

#3


1  

function getKey($array, $key){
    return $array[$key];
}

$var = getKey(myFunc(args), $key);

There is no way to do this without adding a user function unfortunately. It is just not part of the syntax.

没有添加用户功能,没有办法做到这一点。它只是语法的一部分。

You could always just do it the old fashion way

你总是可以用旧式的方式来做

$array = myFunc();
$value = $array[0];

#4


1  

What exactly matches your expecting is:

与您的期望完全匹配的是:

echo pos(array_slice($a=myFunc(), pos(array_keys(array_keys($a), 'NameOfKey'));

answered Kinetix Kin, Taipei

Kinetix Kin,台北回答

#5


1  

if you want this, its probably best to be returning an object (unfortunately, its totally lame php doesnt support this). Heres a crazy way i was able to figure out though, out of novelty (please dont do this!):

如果你想要这个,它可能最好返回一个对象(不幸的是,它完全蹩脚的php不支持这个)。这是一个疯狂的方式我能够弄清楚,出于新奇(请不要这样做!):

function returnsArray(){
    return array("foo" => "bar");
}

echo json_decode(json_encode((object)returnsArray()))->foo;
//prints 'bar'

So yeah..until they add support for array dereferencing in php, i think you should probably just cast the return array as an object:

所以是啊..直到他们在php中添加对数组解除引用的支持,我想你应该把返回数组作为一个对象:

return (object)array("foo" => "bar");

and then you can do returnsArray()->foo, since php relaxes dereferencing for objects but not arrays.. or of course write a wrapper function like others have suggested.

然后你可以做returnArray() - > foo,因为php放松了对象的解除引用而不是数组...或者当然写了一个像其他人建议的包装函数。

#1


10  

As the others have said, you pretty much have to use a temporary variable:

正如其他人所说,你几乎必须使用一个临时变量:

$temp = myFunction();
$value = $temp[0];

But, if know the structure of the array being returned it is possible to avoid the temporary variable.

但是,如果知道返回的数组的结构,则可以避免临时变量。

If you just want the first member:

如果你只想要第一个成员:

$value = reset(myFunction());

If you want the last member:

如果你想要最后一个成员:

$value = end(myFunction());

If you want any one in between:

如果你想要其中任何一个:

// second member
list(, $value) = myFunction();

// third
list(, , $value) = myFunction();

// or if you want more than one:

list(, , $thirdVar, , $fifth) = myFunction();

#2


2  

In PHP, when getting an array as a function result, you unfortunately have to do an extra step:

在PHP中,当将数组作为函数结果获取时,您不得不做一个额外的步骤:

$temp_array = function($args);
$var = $temp_array[0];

For objects, this has been relaxed in PHP 5. You can do:

对于对象,这已经在PHP 5中放宽了。您可以这样做:

$echo function($args)->property;

(provided function returns an object of course.)

(提供的函数当然会返回一个对象。)

#3


1  

function getKey($array, $key){
    return $array[$key];
}

$var = getKey(myFunc(args), $key);

There is no way to do this without adding a user function unfortunately. It is just not part of the syntax.

没有添加用户功能,没有办法做到这一点。它只是语法的一部分。

You could always just do it the old fashion way

你总是可以用旧式的方式来做

$array = myFunc();
$value = $array[0];

#4


1  

What exactly matches your expecting is:

与您的期望完全匹配的是:

echo pos(array_slice($a=myFunc(), pos(array_keys(array_keys($a), 'NameOfKey'));

answered Kinetix Kin, Taipei

Kinetix Kin,台北回答

#5


1  

if you want this, its probably best to be returning an object (unfortunately, its totally lame php doesnt support this). Heres a crazy way i was able to figure out though, out of novelty (please dont do this!):

如果你想要这个,它可能最好返回一个对象(不幸的是,它完全蹩脚的php不支持这个)。这是一个疯狂的方式我能够弄清楚,出于新奇(请不要这样做!):

function returnsArray(){
    return array("foo" => "bar");
}

echo json_decode(json_encode((object)returnsArray()))->foo;
//prints 'bar'

So yeah..until they add support for array dereferencing in php, i think you should probably just cast the return array as an object:

所以是啊..直到他们在php中添加对数组解除引用的支持,我想你应该把返回数组作为一个对象:

return (object)array("foo" => "bar");

and then you can do returnsArray()->foo, since php relaxes dereferencing for objects but not arrays.. or of course write a wrapper function like others have suggested.

然后你可以做returnArray() - > foo,因为php放松了对象的解除引用而不是数组...或者当然写了一个像其他人建议的包装函数。