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.
$Community = rtrim($Community, ',');instead of extra arrray function.print_r($ExplodeCommunity);array_pop()removes last elementprint_r($RemovedLaste);? You've popped an empty string, so print_r() will display an empty string