note 104739 modified in function.array-search by danbrown
| From: | danbrown@php.net | Date: | Tue, 05 Jul 2011 13:39:38 +0000 |
| Subject: | note 104739 modified in function.array-search by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181240@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)
<?php
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
)
*/
?>
--was--
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
)
*/
http://php.net/manual/en/function.array-search.php