0

How can I delete the 3 in "1,2,3,4"?

Dirty hack: Slice the sliced arrays and join them again.

$s = "1,2,3,4";

$array = explode(',', $s);
$a = array_splice($array, 0, -2);
$b = array_splice($array, 1, 1);
$s = implode(',', $a).','.implode($b);

echo $s; // 1,2,4

Test: http://codepad.org/WeDLKvEE

Martin
  • 2,007
  • 6
  • 28
  • 44

3 Answers3

0

OK. How about using strpos and instr? Find the second comma and the third one.

nemmy
  • 753
  • 1
  • 6
  • 19
0
$s = "1,2,3,4";
$array = explode(',', $s); 
unset($array[2]);
$Arr = array_values($array); 
print_r($Arr);

demo : https://eval.in/85585

Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0

How about this, by this you do not depend on the position of the string rather can wipe out by value.

$s = "1,2,3,4";
$array = explode(",",$s);
$val = 3 ;
$key = array_search($val,$array);
if($key!==false){
    unset($array[$key]);
}

echo implode(",",$array); // 1,2,4
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63