Page 1 of 1

Explode and return specific key in one line

Posted: Wed May 02, 2012 2:11 pm
by shiznatix
So this isn't really a problem I am having just thought it would be interesting if it was possible and fun to see what kind of solutions people could come up with.

So, I have a code like this:

Code: Select all

$str = '/one/two/three/four/five/six';//sometimes this can have more the end, such as /seven/eight/ but not always
$pieces = explode('/', $str);
$wantedPiece = $pieces[4];//equals "four" which is the piece I want
So, the question is if it is possible to do it all in 1 line like this:

Code: Select all

$wantedPiece = array_pop(array_pop(explode('/', $str, 6)));//if array_pop returned the remaining array instead of the last element this would work...
Anyone have any ideas? It has to be 1 line and only assign 1 variable which is the 4th position of the return of explode('/', $str);

Re: Explode and return specific key in one line

Posted: Wed May 02, 2012 2:59 pm
by x_mutatis_mutandis_x
Assuming your string always starts with '/' and ends with a value (i.e., doesn't end with /):

Code: Select all

array_pop(array_reverse(explode('/', array_pop(explode('/', $str, i+2)), 2))); //where i is the ith element in the clean array you want to assign to unwanted
In your example you want "four" which is 3rd element (in a clean array(0=>'one',1=>'two',2=>'three',3=>'four')), so i=3

Code: Select all

$unwantedPiece = array_pop(array_reverse(explode('/', array_pop(explode('/', $str, 5)), 2)));

Re: Explode and return specific key in one line

Posted: Wed May 02, 2012 4:13 pm
by pickle
The little used list() construct would be ideal. I actually do use this quite a bit:

Code: Select all

list(,,,,$fourth) = explode ('/',$str);

Re: Explode and return specific key in one line

Posted: Wed May 02, 2012 8:54 pm
by Weiry
pickle wrote:The little used list() construct would be ideal. I actually do use this quite a bit:

Code: Select all

list(,,,,$fourth) = explode ('/',$str);
Love that one pickle!
It wouldn't have occurred to me to leave the options out of list() in order to get to the fourth value.

Re: Explode and return specific key in one line

Posted: Thu May 03, 2012 1:55 am
by shiznatix
Ah cool, I didn't realize you could leave the options out of list(). I had thought of that one but I didn't want to set a bunch of variables that I didn't need / want.

Anyway, cool solutions guys. Thanks

Re: Explode and return specific key in one line

Posted: Thu May 03, 2012 3:56 pm
by x_mutatis_mutandis_x
pickle wrote:The little used list() construct would be ideal. I actually do use this quite a bit:

Code: Select all

list(,,,,$fourth) = explode ('/',$str);
Didn't know you can use list() like that either, but it will work only if you know which element you are trying to obtain. For example, wont work in:

Code: Select all

function parseValueAt($str, $position) { //can't have a one-liner using list
}

Re: Explode and return specific key in one line

Posted: Thu May 03, 2012 4:27 pm
by pickle
The original question specified the key is 4, so I assumed a known index.

Dropping usage of list(), this works:

Code: Select all

function parseValueAt($str, $position) { //can't have a one-liner using list
    return end(array_intersect_key(explode('/',$str),array($position=>$position)));
}

Re: Explode and return specific key in one line

Posted: Fri May 04, 2012 9:12 am
by x_mutatis_mutandis_x
pickle wrote:The original question specified the key is 4, so I assumed a known index.

Dropping usage of list(), this works:

Code: Select all

function parseValueAt($str, $position) { //can't have a one-liner using list
    return end(array_intersect_key(explode('/',$str),array($position=>$position)));
}
Nice.. clever usage of functions than my solution.