note 22455 modified in function.array-splice by vrana

From: Date: Tue, 17 Aug 2004 14:49:56 +0000
Subject: note 22455 modified in function.array-splice by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79963@lists.php.net to get a copy of this message
Please note that array_splice() 's second argument is an OFFSET and not an INDEX. Lets say you want to $array_of_items = array ('nothing','myitem','hisitem','heritem'); $sid = array_search('myitem',$array_of_items); echo $sid; /* prints out 1, since index element 1 is "myitem" */ Now, lets say we want to remove that "myitem" from the array: <?php $array_of_items = array_splice($array_of_items,(1+$sid),1); ?> Notice how you have to add a one to the $sid variable? That is because offset item 1 is "nothing" and since $sid is currently 1 (the index of "myitem"), we add 1 more to it to find out its OFFSET. DO NOT DO THIS: $array_of_items = array_splice($array_of_items,$sid,1); --was-- Please note that array_splice() 's second argument is an OFFSET and not an INDEX. Lets say you want to $array_of_items = array ('nothing','myitem','hisitem','heritem'); $sid = array_search('myitem',$array_of_items); echo $sid; /* prints out 1, since index element 1 is "myitem" */ Now, lets say we want to remove that "myitem" from the array: $array_of_items = array_splice($array_of_items,(1+$sid),1); Notice how you have to add a one to the $sid variable? That is because offset item 1 is "nothing" and since $sid is currently 1 (the index of "myitem"), we add 1 more to it to find out its OFFSET. DO NOT DO THIS: $array_of_items = array_splice($array_of_items,$sid,1); http://php.net/manual/en/function.array-splice.php

« previous php.notes (#79963) next »