Home > Net >  replace specific array value with another array values in php
replace specific array value with another array values in php

Time:12-06

my 1st array values are

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

when array contains value as Other i want to replace that with below array

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

I want to do this in PHP. any help guys?

CodePudding user response:

First loop over the array1 and test for the value 'Other', if found replace the value with your array2

Note the use of &$a to make the $a from the foreach loop a reference, so that it can be used to replace the original array occurance and not a copy of the array which woudl have been the result without the use of the &

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

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

The RESULT

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

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

    [4] => pqr
)

CodePudding user response:

I don't really understand what result are you looking for.

If you want to just replace elements with value 'Other' with some different value:

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

Or if you have an array of replacements, which must gradually replace all 'Other' values:

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

Instead of using foreach ($array as $key => $value) you may also try to replace elements by reference (see RiggsFolly answer)

CodePudding user response:

Based on your request what Yamingue stated is correct. You may need to clarify what you want. Are you attempting to say if the indexed value of the array initial array is 2 and its value is "Other" to replace it with it with the equivalent value of another array with the same index number?

Its been a while since ive done php buy lets give this a go.

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

You may need to unset the values however as i said been a while for me. Nowbif you want to embed the 2nd array into the value of the first array where it currently says other thats another story, each language is a little different on multidimensional and nested arrays so i cant speak to it.

CodePudding user response:

there are several ways to do it as needed. the first is to replace it with another array like this

`

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

$array2 = Array ( 
0 => "lmnsa"
1 => "asda" );

$array1 = $array2

`

  • Related