Link to home
Create AccountLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Removing the last x elements from an array with php

Hi guys hope u r all well and can help.

Ive got some code that is working, but Id like to know if there is another way to do the following, since Im going to have use this functionality somewhere else.

What I am doing is removing the last couple of elements from an array called $mainarea

Im currently doing this by doing the following actions twice......

1) Remove the last element of the array.....
a) explode $mainarea
b) use array_pop on $mainarea and store in $going
c) implode $mainarea

2) Remove the 2nd last element of the array.....
a) explode $mainarea
b) use array_pop on $mainarea and store in $going2
c) implode $mainarea

Guys im just wondering if there is a better way to do this?
Also, does it slow the code execution time by doing what I am doing?

Here is my code snippet...please see below....any help greatly appreciated :>)



// Get rid of the last element from $mainarea ........
$mainarea = explode('_',$mainarea);  
$going = array_pop($mainarea); 
$mainarea = implode('_',$mainarea); 
 
// Get rid of the 2nd last element from $mainarea .............
$mainarea = explode('_',$mainarea);
$going2 = array_pop($mainarea);  
$mainarea = implode('_',$mainarea);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kurijov
kurijov
Flag of Russian Federation image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Simon336697

ASKER

Hi kurijov,
Really appreciate that mate...thank you :>)