note 88896 added to function.array-splice

From: Date: Thu, 12 Feb 2009 13:28:18 +0000
Subject: note 88896 added to function.array-splice
Groups: php.notes 
Request: Send a blank email to php-notes+get-150569@lists.php.net to get a copy of this message
I need <?php array_Splice()?> function, that use array keys instead of order (offset and length) because of associated arrays, and this is result: <?php /** * first variation * * $input is input array * $start is index of slice begin * $end is index of slice end, if this is null, $replacement will be inserted (in the same way as original array_Slice()) *indexes of $replacement are preserved in both examples */ function array_KSplice1(&$input, $start, $end=null, $replacement=null) { $keys=array_Keys($input); $values=array_Values($input); if($replacement!==null) { $replacement=(array)$replacement; $rKeys=array_Keys($replacement); $rValues=array_Values($replacement); } $start=array_Search($start,$keys,true); if($start===false) return false; if($end!==null) { $end=array_Search($end,$keys,true); // if $end not found, exit if($end===false) return false; // if $end is before $start, exit if($end<$start) return false; // index to length $end-=$start-1; } // optional arguments if($replacement!==null) { array_Splice($keys,$start,$end,$rKeys); array_Splice($values,$start,$end,$rValues); } else { array_Splice($keys,$start,$end); array_Splice($values,$start,$end); } $input=array_Combine($keys,$values); return $input; } /** * second variation * * $input is input array * $start is index of slice begin * $length is length of slice, what will be replaced, if is zero, $replacement will be inserted (in the same way as original array_Slice()) */ function array_KSplice2(&$input, $start, $length=0, $replacement=null) { $keys=array_Keys($input); $values=array_Values($input); if($replacement!==null) { $replacement=(array)$replacement; $rKeys=array_Keys($replacement); $rValues=array_Values($replacement); } $start=array_Search($start,$keys,true); if($start===false) return false; // optional arguments if($replacement!==null) { array_Splice($keys,$start,$length,$rKeys); array_Splice($values,$start,$length,$rValues); } else { array_Splice($keys,$start,$length); array_Splice($values,$start,$length); } $input=array_Combine($keys,$values); return $input; } $array=range(1,10); var_Dump(array_KSplice1($array,3,3,array(100=>101,102,103,104))); $array=range(1,10); var_Dump(array_KSplice2($array,3,3,array(100=>101,102,103,104))); ?> Both examples output: array(11) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [100]=> int(101) [101]=> int(102) [102]=> int(103) [103]=> int(104) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) } ---- Server IP: 212.24.129.51 Probable Submitter: 82.142.89.49 ---- Manual Page -- http://www.php.net/manual/en/function.array-splice.php Edit -- https://master.php.net/note/edit/88896 Del: integrated -- https://master.php.net/note/delete/88896/integrated Del: useless -- https://master.php.net/note/delete/88896/useless Del: bad code -- https://master.php.net/note/delete/88896/bad+code Del: spam -- https://master.php.net/note/delete/88896/spam Del: non-english -- https://master.php.net/note/delete/88896/non-english Del: in docs -- https://master.php.net/note/delete/88896/in+docs Del: other reasons-- https://master.php.net/note/delete/88896 Reject -- https://master.php.net/note/reject/88896 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150569) next »