note 106244 modified in function.array-splice by danbrown

From: Date: Fri, 21 Oct 2011 15:11:40 +0000
Subject: note 106244 modified in function.array-splice by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184053@lists.php.net to get a copy of this message
array_splice, split an array into 2 arrays. The returned arrays is the 2nd argument actually and the used array e.g $input here contains the 1st argument of array, e.g <?php $input = array("red", "green", "blue", "yellow"); print_r(array_splice($input, 3)); // Array ( [0] => yellow ) print_r($input); //Array ( [0] => red [1] => green [2] => blue ) ?> if you want to replace any array value do simple like that, first search the array index you want to replace <?php $index = array_search('green', $input);// index = 1 ?> and then use it as according to the definition <?php array_splice($input, $index, 1, array('mygreeen')); //Array ( [0] => red [1] => mygreeen [2] => blue [3] => yellow ) ?> so here green is replaced by mygreen. here 1 in array_splice above represent the number of items to be replaced. so here start at index '1' and replaced only one item which is 'green' --was-- array_splice, split an array into 2 arrays. The returned arrays is the 2nd argument actually and the used array e.g $input here contains the 1st argument of array, e.g $input = array("red", "green", "blue", "yellow"); print_r(array_splice($input, 3)); // Array ( [0] => yellow ) print_r($input); //Array ( [0] => red [1] => green [2] => blue ) if you want to replace any array value do simple like that, first search the array index you want to replace $index = array_search('green', $input);// index = 1 and then use it as according to the definition array_splice($input, $index, 1, array('mygreeen')); //Array ( [0] => red [1] => mygreeen [2] => blue [3] => yellow ) so here green is replaced by mygreen. here 1 in array_splice above represent the number of items to be replaced. so here start at index '1' and replaced only one item which is 'green' http://php.net/manual/en/function.array-splice.php

« previous php.notes (#184053) next »