Why can't I immediately access elements in the array returned by explode()
?
为什么我不能立即访问explode()返回的数组中的元素?
For example, this doesn't work:
例如,这不起作用:
$username = explode('.',$thread_user)[1];
//Parse error: syntax error, unexpected '[
But this code does:
但是这段代码确实:
$username = explode('.',$thread_user);
$username = $username[1];
I don't usually program in PHP, so this is rather confusing to me.
我通常不用PHP编程,所以这对我来说相当混乱。
6 个解决方案
#1
4
Actually, PHP simply does not support this syntax. In languages like Javascript (for instance), the parser can handle more complex nesting/chaining operations, but PHP is not one of those languages.
实际上,PHP根本不支持这种语法。在像Javascript(例如)这样的语言中,解析器可以处理更复杂的嵌套/链接操作,但PHP不是这些语言之一。
#2
6
The reason it isn't obvious how to do what you want is that explode
could return false
. You should check the return value before indexing into it.
不明显如何做你想做的事情的原因是爆炸可能会返回错误。您应该在索引之前检查返回值。
#3
5
It's version dependent. PHP 5.4 does support accessing the returned array.
它取决于版本。 PHP 5.4支持访问返回的数组。
Source: http://php.net/manual/en/language.types.array.php#example-115
资料来源:http://php.net/manual/en/language.types.array.php#example-115
#4
2
Since explode() returns an array, you may use other functions such as $username = current(explode('.',$thread_user));
由于explode()返回一个数组,你可以使用其他函数,如$ username = current(explode('。',$ thread_user));
#5
1
I just use my own function:
我只是使用自己的功能:
function explodeAndReturnIndex($delimiter, $string, $index){
$tempArray = explode($delimiter, $string);
return $tempArray[$index];
}
the code for your example would then be:
您的示例的代码将是:
$username = explodeAndReturnIndex('.', $thread_user, 1);
#6
1
Here's how to get it down to one line:
以下是如何将其归结为一行:
$username = current(array_slice(explode('.',$thread_user), indx,1));
$ username = current(array_slice(explode('。',$ thread_user),indx,1));
Where indx
is the index you want from the exploded array. I'm new to php but I like saying exploded array :)
其中indx是您想要从爆炸数组中获得的索引。我是php新手,但我喜欢说爆炸阵列:)
#1
4
Actually, PHP simply does not support this syntax. In languages like Javascript (for instance), the parser can handle more complex nesting/chaining operations, but PHP is not one of those languages.
实际上,PHP根本不支持这种语法。在像Javascript(例如)这样的语言中,解析器可以处理更复杂的嵌套/链接操作,但PHP不是这些语言之一。
#2
6
The reason it isn't obvious how to do what you want is that explode
could return false
. You should check the return value before indexing into it.
不明显如何做你想做的事情的原因是爆炸可能会返回错误。您应该在索引之前检查返回值。
#3
5
It's version dependent. PHP 5.4 does support accessing the returned array.
它取决于版本。 PHP 5.4支持访问返回的数组。
Source: http://php.net/manual/en/language.types.array.php#example-115
资料来源:http://php.net/manual/en/language.types.array.php#example-115
#4
2
Since explode() returns an array, you may use other functions such as $username = current(explode('.',$thread_user));
由于explode()返回一个数组,你可以使用其他函数,如$ username = current(explode('。',$ thread_user));
#5
1
I just use my own function:
我只是使用自己的功能:
function explodeAndReturnIndex($delimiter, $string, $index){
$tempArray = explode($delimiter, $string);
return $tempArray[$index];
}
the code for your example would then be:
您的示例的代码将是:
$username = explodeAndReturnIndex('.', $thread_user, 1);
#6
1
Here's how to get it down to one line:
以下是如何将其归结为一行:
$username = current(array_slice(explode('.',$thread_user), indx,1));
$ username = current(array_slice(explode('。',$ thread_user),indx,1));
Where indx
is the index you want from the exploded array. I'm new to php but I like saying exploded array :)
其中indx是您想要从爆炸数组中获得的索引。我是php新手,但我喜欢说爆炸阵列:)