note 104739 added to function.array-search

From: Date: Mon, 04 Jul 2011 17:41:28 +0000
Subject: note 104739 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-181228@lists.php.net to get a copy of this message
Array AFTER/BEFORE Function function made to mimic the jQuery function to insert items/arrays into another array before or after a specific key. It is split into two functions for reuse to find the offset of a key. it detects if the array is associative (can also be manually controlled) and adds the keys where needed. if the inserted array is associative then any overlapping keys will be replaced by the new ones. array_insert(array &$original , mixed $insert=null , var $position='after' , $key=null [, $keep_keys=true]) options for $position = (after|before) function key_offset(&$array, $searchKey){ if(is_array($array) && array_key_exists($searchKey, $array)){ $counter = 0; foreach($array as $key => $value){ if($searchKey == $key){ return $counter; } else { $counter++; } } } } function array_insert(&$original, $insert=null, $position='after', $key=null, $sens=true){ if(!empty($insert)){ $after = $position == 'after' ? true : false; $insert = (array)$insert; $assoc = (array_keys($insert) !== range(0, count($insert)-1)); $keep_keys = is_string($insert) || !$assoc || $sens; if(!empty($key) && array_key_exists($key, $original)){ $start = key_offset($original, $key); $start = $after ? $start+1 : $start; if(!$keep_keys){ array_Splice($original,$start,0,$insert); } else { $keys = array_Keys($original); $values = array_Values($original); $insert = (array)$insert; $rKeys = array_Keys($insert); $rValues = array_Values($insert); array_Splice($keys,$start,0,$rKeys); array_Splice($values,$start,0,$rValues); $original = array_Combine($keys,$values); } } else { $original = $after ? array_merge($original, $insert) : array_merge($insert, $original); } } } $insert = array('omg'=> 'it works', 'this_is' => 'awsome', 'r' =>103,104); $insert2 = range(11,15); $insert3 = array(100=>101,102,103,104); $original=range(1,10); array_insert($original, $insert, 'after', 4); $original=range(1,10); array_insert($original, $insert, 'after', 4, false); $original=range(1,10); array_insert($original, $insert2, 'after', 4); $original=range(1,10); array_insert($original, $insert3, 'after', 4); /* Array ( [0] => 104 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [omg] => it works [this_is] => awsome [r] => 103 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => it works [6] => awsome [7] => 103 [8] => 104 [9] => 6 [10] => 7 [11] => 8 [12] => 9 [13] => 10 ) Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [100] => 101 [101] => 102 [102] => 103 [103] => 104 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) */ ---- Server IP: 69.147.83.197 Probable Submitter: 173.242.222.6 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/104739 Del: integrated -- https://master.php.net/note/delete/104739/integrated Del: useless -- https://master.php.net/note/delete/104739/useless Del: bad code -- https://master.php.net/note/delete/104739/bad+code Del: spam -- https://master.php.net/note/delete/104739/spam Del: non-english -- https://master.php.net/note/delete/104739/non-english Del: in docs -- https://master.php.net/note/delete/104739/in+docs Del: other reasons-- https://master.php.net/note/delete/104739 Reject -- https://master.php.net/note/reject/104739 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181228) next »