Why can't I access the exploded array element immediately?
Asked Answered
B

6

7

Why can't I immediately access elements in the array returned by 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.

Balderas answered 15/2, 2010 at 21:43 Comment(0)
L
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.

Lashley answered 15/2, 2010 at 22:4 Comment(2)
^ the real answer. Even if you write a function that returns an invariant array, you still can't index the function call (as James' answer might lead you to believe), because it's simply a matter of the syntax not working.Pomp
+1 for Chuck's comment - clearly some defensive programming has allowed me to avoid learning PHP as fully as I should. (and maybe I shouldn't try to answer questions on the last day of a three-day weekend...)Gyrfalcon
G
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.

Gyrfalcon answered 15/2, 2010 at 21:47 Comment(5)
Thank you. I am not used to return values behaving like that and I missed that when I checked the PHP manual.Balderas
It's always seemed a little quirky to me, but it is useful, and no uglier than e.g. having to check calls to malloc in C for NULL returns.Gyrfalcon
It's actually just that the PHP syntax doesn't support this. You're simply saying it's intended that way to make people check their return values?Aromatic
No, not exactly. Rather, I think this is one of the most useful ramifications of the decision not to support this syntax.Gyrfalcon
James, you're absolutely right that for PHP to support this syntax would add another layer of complexity owing to the fact that in PHP, a soft-typed language, you cannot guarantee the return type of a function. However there are other cases where PHP's parser does in fact allow such conditions may occur. In these cases, PHP simply attempts the closest typecast it can manage and throws a warning message. Try accessing $userID[1] where $userID is an integer, for example.Lashley
M
5

It's version dependent. PHP 5.4 does support accessing the returned array.

Source: http://php.net/manual/en/language.types.array.php#example-115

Masoretic answered 1/9, 2013 at 11:26 Comment(0)
L
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.

Lashley answered 15/2, 2010 at 22:4 Comment(2)
^ the real answer. Even if you write a function that returns an invariant array, you still can't index the function call (as James' answer might lead you to believe), because it's simply a matter of the syntax not working.Pomp
+1 for Chuck's comment - clearly some defensive programming has allowed me to avoid learning PHP as fully as I should. (and maybe I shouldn't try to answer questions on the last day of a three-day weekend...)Gyrfalcon
A
2

Since explode() returns an array, you may use other functions such as $username = current(explode('.',$thread_user));

Arlettaarlette answered 15/2, 2010 at 22:19 Comment(0)
H
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);
Hectorhecuba answered 9/4, 2012 at 16:35 Comment(0)
I
1

Here's how to get it down to one line:

$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 :)

Intertexture answered 6/5, 2012 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.