Forum Moderators: coopster

Message Too Old, No Replies

Displaying the last item in an array via explode()

         

mr_nabo

2:32 pm on Apr 4, 2010 (gmt 0)

10+ Year Member



Hi,

I can't seem to get the last item in an array I'm making from the current URL:

$cs = $_SERVER['REQUEST_URI'];
$currentSection = explode("/",$cs);
$currentSection = end($currentSection);


This is returning nothing, but I know my array has something in it because this displays the correct part of the URL:

$cs = $_SERVER['REQUEST_URI'];
$currentSection = explode("/",$cs);
$currentSection = $currentSection[1];


Is there something I'm missing? php.net didn't seem to show anything related to this...

Thanks

Readie

2:39 pm on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you force a trailing slash, like

http://www.example.com/gallery/photos/

And then explode the $_SERVER['REQUEST_URI'] on /, a print_r will return something like this:

array(
[0] =>
[1] => gallery
[2] => photos
[3] =>
)


So your end() is returning the empty variable.

[edited by: Readie at 2:47 pm (utc) on Apr 4, 2010]

mr_nabo

2:44 pm on Apr 4, 2010 (gmt 0)

10+ Year Member



Hi Readie,

So how would I get the penultimate item in an array (i.e. [2] in your example above?)

This one's got me a bit stumped...

Readie

2:45 pm on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Quite easy really:

$here = explode("/", $_SERVER['REQUEST_URI']);
$num = (count($here) - 1);
echo $here[$num];

mr_nabo

2:54 pm on Apr 4, 2010 (gmt 0)

10+ Year Member



Cool, thanks for this - only thing I needed to change was the -1:

$num = (count($here) - 1);

Readie

3:07 pm on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, of course, it needed to be - 2.

Anyways, good luck with the rest of your project :)

mr_nabo

3:12 pm on Apr 4, 2010 (gmt 0)

10+ Year Member



Thanks Readie, I think it's almost there now.

mooger35

12:55 am on Apr 5, 2010 (gmt 0)

10+ Year Member



Here's another solution which will work for a trailing slash or no trailing slash.

function remove_empty($array)
{
$new_array = array();
foreach ($array as $value)
{
$value = trim($value);
if($value != "")
{
$new_array[] = $value;
}
}
return $new_array;
}

$here = end(remove_empty(explode('/',$_SERVER['REQUEST_URI'])));

Readie

1:01 am on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No need to build your own function, use array_filter:

$here = end(array_filter(explode("/", $_SERVER['REQUEST_URI'])));
echo $here;

[edited by: Readie at 1:10 am (utc) on Apr 5, 2010]

mooger35

1:03 am on Apr 5, 2010 (gmt 0)

10+ Year Member



I actually did try array_filter but get no value back.


/edit... nevermind. My own typo in there.

Readie

1:08 am on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, bit wierd.

Unfortunatley I can't test this for trailing slashes as I've configured all of my sites to not have a trailing slash.

Sorry to call doubt on you, but did you check for typos etc.?

[edit]

Hehe, we've all done it - I've never written more than a few lines without a typing error in the code somewhere :)

[edited by: Readie at 1:09 am (utc) on Apr 5, 2010]

mooger35

1:09 am on Apr 5, 2010 (gmt 0)

10+ Year Member



The typo was why I gave up on array_filter in the first place... only to go back to user $_SERVER on the next try. I also had $_REQUEST which didn't work.

:-)