搜索键替换值

时间:2012-08-30 10:40:08

标签: php arrays

假设我有一个数组:

$myArray = array('alfa' => 'apple', 'bravo' => 'banana', 'charlie' => 'cherry', 'delta' => 'danjou pear');

我想搜索某些键,如果找到则替换其他数组的值:

$myReplacements = array('bravo' => 'bolognese', 'lie' => 'candy');

我知道我可以这样做:

function array_search_replace($haystack, $replacements, $exactMatch = false) {
    foreach($haystack as $haystackKey => $haystackValue) {
        foreach($replacements as $replacementKey => $replacementValue) {
            if($haystackKey == $replacementKey || (!$exactMatch && strpos($haystackKey, $replacementKey) !== false)) {
                $haystack[$haystackKey] = $replacementValue;
            }
        }
    }
    return $haystack;
}

但是,真的没有更聪明/更快的方法吗?

编辑:我还需要能够搜索关键部分,这样'谎言'和'查理'就会产生匹配。

EDIT2:预期结果如下:

var_dump(array_search_replace($myArray, $myReplacements));

array(4) { ["alfa"]=> string(5) "apple" ["bravo"]=> string(9) "bolognese" ["charlie"]=> string(5) "candy" ["delta"]=> string(11) "danjou pear" }

var_dump(array_search_replace($myArray, $myReplacements, true));

array(4) { ["alfa"]=> string(5) "apple" ["bravo"]=> string(9) "bolognese" ["charlie"]=> string(6) "cherry" ["delta"]=> string(11) "danjou pear" }

4 个答案:

答案 0 :(得分:2)

您可以使用PHP的array_replace功能。

此外,array_merge做了类似的事情。

在您的代码中:

$finalArray = array_replace($myArray, $myReplacements);

完整的PHP代码

<?php
    $myArray = array('alfa' => 'apple', 'bravo' => 'banana', 'charlie' => 'cherry', 'delta' => 'danjou pear');
    $myReplacements = array('bravo' => 'bolognese', 'lie' => 'candy');
    $finalArray = array_replace($myArray, $myReplacements);
    print_r($finalArray);
?>

输出

Array
(
    [alfa] => apple
    [bravo] => bolognese
    [charlie] => cherry
    [delta] => danjou pear
    [lie] => candy
)

在这里小提琴:http://codepad.viper-7.com/60o5xT

答案 1 :(得分:1)

这是你需要的吗?

<?php
$myArray = array('alfa' => 'apple', 'bravo' => 'banana', 'charlie' => 'cherry', 'delta' => 'danjou pear');
$myReplacements = array('bravo' => 'bolognese', 'lie' => 'candy');
print_r(array_merge($myArray, $myReplacements));
?>

输出:

Array
(
    [alfa] => apple
    [bravo] => bolognese
    [charlie] => cherry
    [delta] => danjou pear
    [lie] => candy
)

使用array_merge

答案 2 :(得分:0)

您可以使用以下代码直接替换匹配的密钥

<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");

$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
?>

<强>输出:

Array
(
    [0] => grape
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => cherry
)

答案 3 :(得分:0)

PHP中的数组是哈希值,您不需要搜索密钥,可以在O(1)时间内访问它。

$myArray = array('alfa' => 'apple', 'bravo' => 'banana', 'charlie' => 'cherry', 'delta' => 'danjou pear');
$myReplacements = array('bravo' => 'bolognese', 'lie' => 'candy');

var_dump($myArray);

foreach($myReplacements as $k => $v) {
  if(array_key_exists($k, $myArray)) {
    $myArray[$k] = $v;
  }
}

var_dump($myArray);

输出:

array(4) {
  ["alfa"]=>
  string(5) "apple"
  ["bravo"]=>
  string(6) "banana"
  ["charlie"]=>
  string(6) "cherry"
  ["delta"]=>
  string(11) "danjou pear"
}
array(4) {
  ["alfa"]=>
  string(5) "apple"
  ["bravo"]=>
  string(9) "bolognese"
  ["charlie"]=>
  string(6) "cherry"
  ["delta"]=>
  string(11) "danjou pear"
}