Array slice function that works with associative arrays (keys):
function array_slice_assoc($array,$keys) {
return array_intersect_key($array,array_flip($keys));
}(PHP 4, PHP 5, PHP 7, PHP 8)
array_slice — 配列の一部を展開する
array_slice()は、array
から引数 offset および
length で指定された連続する要素を返します。
array入力の配列。
offset
offset が負の値ではない場合、要素位置の計算は、
配列 array の offset から始められます。
offset が負の場合、要素位置の計算は
array の最後から行われます。
offset は、配列の位置を表します。配列のキーではありません。
length
lengthが指定され、正の場合、
配列の要素の中から最大でその数までの要素を返します。
配列の要素数が length より少ない場合は、
配列から取得できる要素だけを返します。
length
が指定され、負の場合、配列の末尾からその数だけ要素を取り除いた配列が返されます。
省略された場合、offset
から配列の最後までの全ての要素が返されます。
preserve_keys注意:
array_slice() はデフォルトで配列の数値キーを並べなおし、 リセットします。
preserve_keysをtrueにする事でこの動作を変更することができます。 文字列のキーは、このパラメータの値にかかわらず常に保存されます。
切り取った部分を返します。オフセットが配列のサイズより大きい場合は、空の配列を返します。
例1 array_slice() の例
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // "c", "d", "e" を返す
$output = array_slice($input, -2, 1); // "d" を返す
$output = array_slice($input, 0, 3); // "a", "b", "c" を返す
// 配列キーの違いに注意
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>上の例の出力は以下となります。
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
例2 array_slice() と、キーが1から始まる配列
<?php
$input = array(1 => "a", "b", "c", "d", "e");
print_r(array_slice($input, 1, 2));
?>上の例の出力は以下となります。
Array
(
[0] => b
[1] => c
)
例3 array_slice() に、数値と文字列のキーが混じった配列を渡す例
<?php
$ar = array('a'=>'apple', 'b'=>'banana', '42'=>'pear', 'd'=>'orange');
print_r(array_slice($ar, 0, 3));
print_r(array_slice($ar, 0, 3, true));
?>上の例の出力は以下となります。
Array
(
[a] => apple
[b] => banana
[0] => pear
)
Array
(
[a] => apple
[b] => banana
[42] => pear
)
Array slice function that works with associative arrays (keys):
function array_slice_assoc($array,$keys) {
return array_intersect_key($array,array_flip($keys));
}<?php
// CHOP $num ELEMENTS OFF THE FRONT OF AN ARRAY
// RETURN THE CHOP, SHORTENING THE SUBJECT ARRAY
function array_chop(&$arr, $num)
{
$ret = array_slice($arr, 0, $num);
$arr = array_slice($arr, $num);
return $ret;
}If you want an associative version of this you can do the following:
function array_slice_assoc($array,$keys) {
return array_intersect_key($array,array_flip($keys));
}
However, if you want an inverse associative version of this, just use array_diff_key instead of array_intersect_key.
function array_slice_assoc_inverse($array,$keys) {
return array_diff_key($array,array_flip($keys));
}
Example:
$arr = [
'name' => 'Nathan',
'age' => 20,
'height' => 6
];
array_slice_assoc($arr, ['name','age']);
will return
Array (
'name' = 'Nathan',
'age' = 20
)
Where as
array_slice_assoc_inverse($arr, ['name']);
will return
Array (
'age' = 20,
'height' = 6
)based on worldclimb's arem(), here is a recursive array value removal tool that can work with multidimensional arrays.
function remove_from_array($array,$value){
$clear = true;
$holding=array();
foreach($array as $k => $v){
if (is_array($v)) {
$holding [$k] = remove_from_array ($v, $value);
}
elseif ($value == $v) {
$clear = false;
}
elseif($value != $v){
$holding[$k]=$v; // removes an item by combing through the array in order and saving the good stuff
}
}
if ($clear) return $holding; // only pass back the holding array if we didn't find the value
}array_slice can be used to remove elements from an array but it's pretty simple to use a custom function.
One day array_remove() might become part of PHP and will likely be a reserved function name, hence the unobvious choice for this function's names.
<?
function arem($array,$value){
$holding=array();
foreach($array as $k => $v){
if($value!=$v){
$holding[$k]=$v;
}
}
return $holding;
}
function akrem($array,$key){
$holding=array();
foreach($array as $k => $v){
if($key!=$k){
$holding[$k]=$v;
}
}
return $holding;
}
$lunch = array('sandwich' => 'cheese', 'cookie'=>'oatmeal','drink' => 'tea','fruit' => 'apple');
echo '<pre>';
print_r($lunch);
$lunch=arem($lunch,'apple');
print_r($lunch);
$lunch=akrem($lunch,'sandwich');
print_r($lunch);
echo '</pre>';
?>
(remove 9's in email)The documentation doesn't say it, but if LENGTH is ZERO, then the result is an empty array [].remember that array_slice returns an array with the current element. you must use array_slice($array, $index+1) if you want to get the next elements.Using the varname function referenced from the array_search page, submitted by dcez at land dot ru. I created a multi-dimensional array splice function. It's usage is like so:
$array['admin'] = array('blah1', 'blah2');
$array['voice'] = array('blah3', 'blah4');
array_cut('blah4', $array);
...Would strip blah4 from the array, no matter where the position of it was in the array ^^ Returning this...
Array ( [admin] => Array ( [0] => blah1 [1] => blah2 ) [voice] => Array ( [0] => blah3 ) )
Here is the code...
<?php
function varname ($var)
{
// varname function by dcez at land dot ru
return (isset($var)) ? array_search($var, $GLOBALS) : false;
}
function array_cut($needle, $haystack)
{
foreach ($haystack as $k => $v)
{
for ($i=0; $i<count($v); $i++)
if ($v[$i] === $needle)
{
return array_splice($GLOBALS[varname($haystack)][$k], $i, 1);
break; break;
}
}
?>
Check out dreamevilconcept's forum for more innovative creations!