array_splice() for associative arrays

2020-01-24 07:29发布

Say I have an associative array:

array(
  "color" => "red",
  "taste" => "sweet",
  "season" => "summer"
);

and I want to introduce a new element into it:

"texture" => "bumpy" 

behind the 2nd item but preserving all the array keys:

array(
  "color" => "red",
  "taste" => "sweet",
  "texture" => "bumpy", 
  "season" => "summer"
);

is there a function to do that? array_splice() won't cut it, it can work with numeric keys only.

标签: php arrays
13条回答
一夜七次
2楼-- · 2020-01-24 08:12

Important Note:It won't work if the keys are not in the array.

My solution is (I love using native php array functions);

$startIndex = array_search($firstKey, array_keys($arr);
$endIndex = array_search($secondKey, array_keys($arr));
array_splice($arr, $startIndex, $endIndex - $startIndex);

It is pretty simple and you can turn it into a function easily.

查看更多
该账号已被封号
3楼-- · 2020-01-24 08:16

Here's another way:

function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
  $keys = array_keys($input);
  $values = array_values($input);
  array_splice($keys, $offset, $length, array_keys($replacement));
  array_splice($values, $offset, $length, array_values($replacement));
  $input = array_combine($keys, $values);
}
查看更多
聊天终结者
4楼-- · 2020-01-24 08:17

There is a super simple way to do this that I came up with tonight.It will search for a key, splice it like normal and return the removed element like the normal function.

function assoc_splice($source_array, $key_name, $length, $replacement){
    return array_splice($source_array, array_search($key_name, array_keys($source_array)), $length, $replacement);
}
查看更多
家丑人穷心不美
5楼-- · 2020-01-24 08:17

This works like array_splice, but preserves the keys of the inserted array:

    function array_splicek(&$array, $offset, $length, $replacement) {
        if (!is_array($replacement)) {
            $replacement = array($replacement);
        }
        $out = array_slice($array, $offset, $length);
        $array = array_slice($array, 0, $offset, true) + $replacement
            + array_slice($array, $offset + $length, NULL, true);
        return $out;
    }

You use this as you would array_splice, but just add a k at the end. (ragulka's answer is good, but this makes it easier to adapt existing code.) So, for example

$a = array("color" => "red", "taste" => "sweet", "season" => "summer");
$b = array("texture" => "bumpy");

Instead of

array_splice($a, 2, 0, $b);

use

array_splicek($a, 2, 0, $b);

Then $a will contain the result you're looking for.

查看更多
在下西门庆
6楼-- · 2020-01-24 08:23

I think you need to do that manually:

# Insert at offset 2
$offset = 2;
$newArray = array_slice($oldArray, 0, $offset, true) +
            array('texture' => 'bumpy') +
            array_slice($oldArray, $offset, NULL, true);
查看更多
劫难
7楼-- · 2020-01-24 08:27

My solution mimics array_splice exactly, the second parameter is now String $key, instead of Int $offset,

function array_splice_assoc ( &$input ,$key, $length = 0 , $replacement = null ){

    $keys = array_keys( $input );
    $offset = array_search( $key, $keys );

    if($replacement){
        $values = array_values($input);
        $extracted_elements = array_combine(array_splice($keys, $offset, $length, array_keys($replacement)),array_splice($values, $offset, $length, array_values($replacement)));
        $input = array_combine($keys, $values);
    } else {
        $extracted_elements = array_slice($input, $offset, $length);
    }
    return $extracted_elements;
}

So to get the result you require you would do

$array = array(
  "color" => "red",
  "taste" => "sweet",
  "season" => "summer"
);
$replacement = array("texture" => "bumpy");


array_splice_assoc ($array ,"season", 0, $replacement);

Output

Array
(
    [color] => red
    [taste] => sweet
    [texture] => bumpy
    [season] => summer
)
查看更多
登录 后发表回答