【问题标题】:replace specific array value with another array values in php用php中的另一个数组值替换特定的数组值
【发布时间】:2022-01-11 01:42:15
【问题描述】:

我的第一个数组值是

Array ( 
[0] => abc 
[1] => xyz 
[2] => Other 
[3] => Other 
[4] => pqr )

当数组包含其他值时,我想用下面的数组替换它

Array ( 
[0] => lmnsa 
[1] => asda )

我想在 PHP 中执行此操作。有帮助吗?

【问题讨论】:

  • 到目前为止您尝试过什么?在您的问题中发布您的代码。
  • 我是 PHP 新手,一直卡在这部分,所以需要专家帮助
  • 社区不会为你编写代码。您编写代码,当您遇到困难并且无法正常工作时,您发布该代码,然后社区会尝试提供帮助。

标签: php arrays json


【解决方案1】:

首先循环 array1 并测试值 'Other',如果找到,则将值替换为您的 array2

注意使用&$a 使foreach 循环中的$a 成为引用,这样它就可以用来替换原始数组的出现而不是数组的副本不使用& 会是结果

$array1 = Array ( "abc","xyz","Other", "Other", "pqr" );
$array2 = Array ( "lmnsa", "asda" );

foreach ($array1 as &$a) {
    if ( $a == 'Other') {
        $a = $array2;
    }
}
print_r($array1);

结果

Array
(
    [0] => abc
    [1] => xyz
    [2] => Array
        (
            [0] => lmnsa
            [1] => asda
        )

    [3] => Array
        (
            [0] => lmnsa
            [1] => asda
        )

    [4] => pqr
)

【讨论】:

    【解决方案2】:

    我不太明白你在寻找什么结果。

    如果您只想将值为“其他”的元素替换为不同的值:

    $newValue = 'New value instead of Other';
    // $newValue = ['abc' 'def']; <-- if you want to replace string by an array. It wasn't unclear what you expect to achieve from the question.
    
    $array = ['a', 'b', 'Other', 'c', 'Other', 'd'];
    foreach ($array as $idx => $element) {
        if ($element === 'Other') {
            $array[$idx] = $newValue;
        }
    }
    

    或者,如果您有一个替换数组,必须逐渐替换所有“其他”值:

    $array = ['a', 'b', 'Other', 'c', 'Other', 'd'];
    $replacements = ['New Value 1', 'New Value 2'];
    
    $replacementIdx = 0;
    foreach ($array as $idx => $element) {
        // Always check if you were not run out of replacement values
        if (!isset($replacements[$replacementIdx])) {
            break;
        }
    
        if ($element === 'Other') {
            $array[$idx] = $replacements[$replacementIdx++];
        }
    }
    

    除了使用 foreach ($array as $key =&gt; $value) 您还可以尝试通过引用替换元素(请参阅 RiggsFolly 答案)

    【讨论】:

    • 非常感谢@Hast。第二个解决方案是我想要的。现在我的代码可以正常工作了
    • 我已经更正了一些缺少的括号和分号$array = ['a', 'b', 'Other', 'c', 'Other', 'd']; $replacements = ['New Value 1', 'New Value 2']; $replacementIdx = 0; foreach ($array as $idx =&gt; $element) { // Always check if you were not run out of replacement values if (!isset($replacements[$replacementIdx])) { break; } if ($element === 'Other') { $array[$idx] = $replacements[$replacementIdx++]; } } print_r($array);
    • 修复了答案,谢谢
    【解决方案3】:

    根据您的要求,Yamingue 所说的内容是正确的。你可能需要澄清你想要什么。您是否试图说数组初始数组的索引值为 2 并且其值为“Other”以将其替换为具有相同索引号的另一个数组的等效值?

    自从我完成 php buy 已经有一段时间了,让我们试试吧。

    $array1 = Array ( 
    0 => "abc"
    1 => "xyz" 
    2 => "Other" 
    3 => "Other" 
    4 => "pqr" );
    
        $array2 = Array ( 
        0 => "lmnsa"
        1 => "asda" 
        2 => "thg"
        3 => "ris"
        4 => "slrn");
    
        Foreach($array1 as $arr1key => $arr1val) {
        If($arr1val == "Other"){
        $array1[$arr1key] = $array2[$arr1key];
        }
        }
    

    您可能需要取消设置值,但正如我所说的那样对我来说已经有一段时间了。现在,如果你想将第二个数组嵌入到第一个数组的值中,它目前说的是另一回事,每种语言在多维和嵌套数组上都有点不同,所以我不能说。

    【讨论】:

      【解决方案4】:

      有几种方法可以根据需要进行。第一个是用另一个这样的数组替换它

      `

      $array1 = Array ( 
      0 => "abc"
      1 => "xyz" 
      2 => "Other" 
      3 => "Other" 
      4 => "pqr" );
      
      $array2 = Array ( 
      0 => "lmnsa"
      1 => "asda" );
      
      $array1 = $array2
      

      `

      【讨论】:

      • 您提供的解决方案是用第二个替换整个数组。我只想在有“其他”时替换特定值。
      猜你喜欢
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2021-01-27
      • 1970-01-01
      • 2015-03-24
      相关资源
      最近更新 更多