-1

I have an string

 $Community = "1,2,3,4,";
 $ExplodeCommunity = explode(',',$Community);//Split 
 print_r($ExplodeCommunity);

Which gives

  Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => ) 

Now i want to remove last element in array. i tried with array_pop

  $RemovedLaste = array_pop($ExplodeCommunity);

  print_r($RemovedLaste);

But Nothing is getting Printed. How to remove the last element from array without using array_pop

7
  • 1
    Why not just use $Community = rtrim($Community, ','); instead of extra arrray function. Commented Sep 8, 2017 at 11:15
  • 1
    Your code works fine, you are trying to print an empty element. print the original array instead to confirm it worked: print_r($ExplodeCommunity); Commented Sep 8, 2017 at 11:17
  • 1
    in this case array_pop() removes last element Commented Sep 8, 2017 at 11:17
  • 1
    What do you expect to see printed when you do print_r($RemovedLaste);? You've popped an empty string, so print_r() will display an empty string Commented Sep 8, 2017 at 11:20
  • 1
    your last element is empty Commented Sep 8, 2017 at 11:59

8 Answers 8

3

array_pop uses a reference so it will affect the original array... try this ...

$x=array_pop($ExplodeCommunity);
print_r($ExplodeCommunity);
var_dump($x);

and see what you get , btw you want to print empty value ?

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

5 Comments

and it prints array without last element but we need print last element
@ArtemIlchenko which is what the title suggests the op wants
The last entry in ExplodeCommunity is empty so there is nothing to print?
OP strictly asked last element from array without using array_pop
OP is not aware that their code already works and will behave in the very same way whatever function or language will use instead. The question should be closed.
2
   function remove_last($Community) {

            $ExplodeCommunity = explode(',',$Community);
            $remove_empty = array_values(array_filter($ExplodeCommunity));
            $last_value_in_array =  end($remove_empty); 
            $lengh = sizeof($remove_empty) -1 ;
            unset($remove_empty[$lengh]);   
            return $remove_empty; 
    }

    $Community = "1,2,3,4,";
    print_r(remove_last($Community)); 

    Output : Array ( [0] => 1 [1] => 2 [2] => 3 )

1 Comment

OP strictly asked last element from array without using array_pop
2

Problem found

The problem you have is the last comma , in your string '1,2,3,4,' when you explode using the code you provided below:

$Community = "1,2,3,4,";
                   //^ The value after this is an empty string ''
$ExplodeCommunity = explode(',',$Community); 

This results in $ExplodeCommunity essentially being this, where the last entry is an empty string:

$ExplodeCommunity = [1,2,3,4,''] 

That is why you end up with 5 entries in your Array

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => ) 
                                                  ^ This is an empty string

The function array_pop returns the value of the last entry in that Array which in this case is an empty string ''.

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints an empty string ''

The value of $ExplodeCommunity is:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4) 


Solution 1: Keeping the $Community value the same:

If you do array_pop twice on $ExplodeCommunity you will get 4:

$Community = "1,2,3,4,";
                   //^ The value after this is an empty string ''
$ExplodeCommunity = explode(',',$Community); 

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints an empty string ''

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints 4

The value of $ExplodeCommunity is:

Array ( [0] => 1 [1] => 2 [2] => 3) 


Solution 2: Using rtrim to remove ending comma ,:

You can use rtrim like the below to remove the ending comma from the $Community string:

$Community = rtrim("1,2,3,4,", ',');
$ExplodeCommunity = explode(',',$Community); 

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints 4

The value of $ExplodeCommunity is:

Array ( [0] => 1 [1] => 2 [2] => 3) 

Other Solutions

You can use other functions such as unset/array_slice as described in other answers.

1 Comment

This should be flagged as the solution of the OP. Really.
0

You can use unset

unset($ExplodeCommunity[count($ExplodeCommunity) - 1] )

Comments

0

try this...

if(empty($ExplodeCommunity[count($ExplodeCommunity)-1])) {
    unset($ExplodeCommunity[count($ExplodeCommunity)-1]);
}

1 Comment

Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
0

Give this a try:

$arr = array('hello', 'world', '2');
$arr = unset(end($arr));

This is going to 'unset' the 'end' element of the array.

Comments

0

You can also try with array_slice

array_slice : returns the sequence of elements from the array array as specified by the offset and length parameters.

$RemovedLaste = array_slice(ExplodeCommunity, 0,-1);
print_r($RemovedLaste);

Comments

0

since php 4, you can remove the last element using explode function like so :

$Community = "1,2,3,4,";
$ExplodeCommunity = explode(',', $Community,-1);
print_r($ExplodeCommunity);

that will give you :

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4)

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.