0

假设我有一个数组:

$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;
}

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

编辑:我还需要能够搜索关键部分,以便 'lie' 和 'charlie' 匹配。

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

4 回答 4

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

于 2012-08-30T10:42:02.120 回答
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

于 2012-08-30T10:45:38.937 回答
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
)
于 2012-08-30T10:47:52.647 回答
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"
}
于 2012-08-30T10:50:03.050 回答