Re: array question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas has it. You can use the preg_match function to find it. I would
use regexp for that reason. regexp is good for making sure things are typed
the way they need to (mostly used for).

Ravi.


On Sat, Dec 18, 2010 at 5:17 PM, Jim Lucas <lists@xxxxxxxxx> wrote:

> On 12/17/2010 12:52 PM, Sorin Buturugeanu wrote:
>
>> Hello all!
>>
>> 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.
>>
>> Cheers and thanks!
>>
>>
> Sure it CAN be done.  Nobody laugh too loud here... But...
>
> <?php
>
> $s = 'banana,apple,mellon,grape,nut,orange';
> echo preg_replace('/([^,]+,){3}([^,]+).*/', '$2', $s);
>
> ?>
> Outputs: grape
>
> The {3} part is equivalent to the array position.  Change that number, and
> you change which word will get displayed.
>
> Jim Lucas
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux