RE: [PHP] array question

From: Date: Fri, 17 Dec 2010 21:24:12 +0000
Subject: RE: [PHP] array question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-310123@lists.php.net to get a copy of this message
[snip] I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. [/snip] Because the array is not formed until after the explode you cannot do it with one command, but you could place 2 commands on one line :) $a = explode(',', $s); echo $a[2];

« previous php.general (#310123) next »