Explode and return specific key in one line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Explode and return specific key in one line

Post 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);
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Explode and return specific key in one line

Post 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)));
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Explode and return specific key in one line

Post 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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Explode and return specific key in one line

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Explode and return specific key in one line

Post 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
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Explode and return specific key in one line

Post 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
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Explode and return specific key in one line

Post 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)));
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Explode and return specific key in one line

Post 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.
Post Reply