2

Can't wrap my head around this...

Say, we explode the whole thing like so:

$extract = explode('tra-la-la', $big_sourse);

Then we want to get a value at index 1:

$finish = $extract[1];

My question is how to get it in one go, to speak so. Something similar to this:

$finish = explode('tra-la-la', $big_sourse)[1]; // does not work

Something like the following would work like a charm:

$finish = end(explode('tra-la-la', $big_sourse));

// or

$finish = array_shift(explode('tra-la-la', $big_sourse));

But what if the value is sitting somewhere in the middle?

4
  • I don't think that there is a solution, like you are searching for. Why don't you want to store the extracted data into an array before? Why the direct call? Memory-saving-issues? Commented Jun 23, 2010 at 7:17
  • I just love the solutions with end and array_shift, so I would like something similar to those in case the value is in the middle. But memory could be an issue too, by the way. Commented Jun 23, 2010 at 7:28
  • 2
    possible duplicate of Access array element from function call in php Commented Jun 23, 2010 at 7:32
  • I don't think this is a dupe, but the link matters. So 'great comment' to you! ;) Commented Jun 23, 2010 at 13:53

4 Answers 4

7

Function Array Dereferencing has been implemented in PHP 5.4. For older version that's a limitation in the PHP parser that was fixed in here, so no way around it for now I'm afraid.

Sign up to request clarification or add additional context in comments.

5 Comments

Dereferencing arrays from function calls was fixed recently? When?
I wasn't aware that it had been fixed! Do you have a link to where this fix was announced?
This is not in an official release yet though, is it?
@Ignacio you really made my day with that news
Wow... that will change my whole PHP experience! Not sure if I am ready for such a change ;)
1

Something like that :

end(array_slice(explode('tra-la-la', $big_sourse), 1, 1));

Though I don't think it's better/clearer/prettier than writing it on two lines.

Comments

0

you can use list:

list($first_element) = explode(',', $source);

[1] would actually be the second element in the array, not sure if you really meant that. if so, just add another variable to the list construct (and omit the first if preferred)

list($first_element, $second_elment) = explode(',', $source);
// or
list(, $second_element) = explode(',', $source);

2 Comments

It's all fun and games until someone tries to get the nth element.
@ignacio: well, then it's best to save the array, do bound checks and then retrieve the value. you can always write another function for that ;)
0

My suggest - yes, I've figured out something -, would be to use an extra agrument allowed for the function. If it is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. So, if we want to get, say, a value at index 2 (of course, we're sure that the value we like would be there beforehand), we just do it as follows:

$finish = end(explode('tra-la-la', $big_sourse, 3)); 

explode will return an array that contains a maximum of three elements, so we 'end' to the last element which the one we looked for, indexed 2 - and we're done!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.