array_splice保留键 [英] array_splice preserving keys

查看:295
本文介绍了array_splice保留键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了使用保留键拼接数组的情况,因此我做了以下功能.
我达到了用数组包装每个项目的解决方案,但是似乎存在一些内存效率低下的语句.
有什么想法吗?
谢谢.

I faced the situation that splicing arrays with preserved-keys, so I made the following function.
I reached the solution that wrapping each items with array, but there seems to be some memory-inefficient statements.
Have you any ideas?
Thank you.

array_splice不同,这保留了密钥.

This preserves keys, differently from array_splice.

  • &$input ->与array_splice相同.
  • $key ->目标密钥.
  • $use_key_as_offset ->使用 $key 参数作为数字偏移量.
  • $length ->与array_splice相同.
  • $replacement ->与array_splice相同.但是您还可以为每个值提供.
  • &$input -> same as array_splice one.
  • $key -> target key.
  • $use_key_as_offset -> use $key parameter as a numeric offset.
  • $length -> same as array_splice one.
  • $replacement -> same as array_splice one. But you can also provide key for each value.
function array_splice_pk(&$input, $key, $use_key_as_offset = false, $length = 0, $replacement = null) {
    if (!is_array($input) || !is_scalar($key)) {
        return array();
    }
    if ($replacement !== null) {
        $replacement = array($replacement);
        if (!is_array($replacement[0])) {
            $replacement = array($replacement);
        }
    }
    $wrapper = array();
    foreach ($input as $k => $v) {
        $wrapper[] = array($k => $v);
    }
    $del_key = null;
    foreach ($wrapper as $k => $v) {
        if ($use_key_as_offset) {
            if ($k === (int)$key) {
                $del_key = $k;
                break;
            }
        } else {
            if (key($v) == $key) {
                $del_key = $k;
                break;
            }
        }
    }
    if ($del_key === null) {
        return array();
    }
    if ($replacement === null) {
        $wrapper_ret = array_splice($wrapper, $del_key, $length);
    } else {
        $wrapper_ret = array_splice($wrapper, $del_key, $length , $replacement);
    }
    $ret = $input = array();
    foreach ($wrapper_ret as $wrap) {
        list($k, $v) = each($wrap);
        $ret[$k] = $v;
    }
    foreach ($wrapper as $wrap) {
        list($k ,$v) = each($wrap);
        $input[$k] = $v;
    }
    return $ret;
}

示例:

$arr1 = $arr2 = array(
    'one'   => 'test',
    'two'   => 'test',
    'three' => 'test',
    'four'  => 'test',
);
$ret1 = array_splice_pk($arr1, 'three', false, 1, array('fizz' => '!!!'));
$ret2 = array_splice_pk($arr2, 2      , true , 1, array('fizz' => '!!!'));

var_dump('Result1', $arr1, $ret1, 'Result2', $arr2, $ret2);

结果:

string(7) "Result1"
array(4) {
  ["one"]=>
  string(4) "test"
  ["two"]=>
  string(4) "test"
  ["fizz"]=>
  string(3) "!!!"
  ["four"]=>
  string(4) "test"
}
array(1) {
  ["three"]=>
  string(4) "test"
}
string(7) "Result2"
array(4) {
  ["one"]=>
  string(4) "test"
  ["two"]=>
  string(4) "test"
  ["fizz"]=>
  string(3) "!!!"
  ["four"]=>
  string(4) "test"
}
array(1) {
  ["three"]=>
  string(4) "test"
}

推荐答案

我在 array_slice .

<?php
function array_splice_assoc(&$input, $offset, $length, $replacement = array()) {
    $replacement = (array) $replacement;
    $key_indices = array_flip(array_keys($input));
    if (isset($input[$offset]) && is_string($offset)) {
            $offset = $key_indices[$offset];
    }
    if (isset($input[$length]) && is_string($length)) {
            $length = $key_indices[$length] - $offset;
    }

    $input = array_slice($input, 0, $offset, TRUE)
            + $replacement
            + array_slice($input, $offset + $length, NULL, TRUE); 
}

 $fruit = array(
    'orange' => 'orange',
    'lemon' => 'yellow',
    'lime' => 'green',
    'grape' => 'purple',
    'cherry' => 'red',
 );

  // Replace lemon and lime with apple
  array_splice_assoc($fruit, 'lemon', 'grape', array('apple' => 'red'));

  // Replace cherry with strawberry
  array_splice_assoc($fruit, 'cherry', 1, array('strawberry' => 'red'));
  ?>

在保留键的同时,看起来更节省空间和时间.

It appears more space and time efficient while it preserves the keys.

这篇关于array_splice保留键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆