Search code examples
phparraysarray-push

insert value to php array with index


Hello I have this array :

array(3) {
  [0]=>
  string(2) "35"
  [1]=>
  string(2) "33"
  [2]=>
  string(2) "50"
}

That I obtain using this :

$groupstring = $_POST['groupstring'];
$groupstring =  stripslashes($groupstring);
$blah = unserialize($groupstring);

var_dump($blah);

this is what's inside the var groupstring : a:3:{i:0;s:2:"35";i:1;s:2:"33";i:2;s:2:"50";}

I want to insert a new value for example 38 at the end. I tried array_push($array, '38') but it failed I understand there is an index. what would be the best way to do it? thanks much!!


Solution

  • $groupstring = $_POST['groupstring'];
    $groupstring =  stripslashes($groupstring);
    $blah = unserialize($groupstring);
    
    $blah[] = 38;
    var_dump($blah);