3

I'm trying to use str_replace() to remove the "my." out of the value4 elements in an array of arrays.

However, str_replace("my.", "", $myarray); isn't changing anything.

Does str_replace() not work on two-dimensional arrays?

My sample data and coding attempt:

$array = [
    [
        'value1' => 'John Doe',
        'value2' => 'Father',
        'value3' => '',
        'value4' => 'http://www.website.my.com'
    ],
    [
        'value1' => 'Jane Doe',
        'value2' => 'Mother',
        'value3' => '',
        'value4' => 'http://www.website.my.com'
    ]
    // ...
];
$out = str_replace('.my', '', $array);
var_export($out);
1
  • @MarkBaker It can also be used with arrays, just not multidimensional Commented Jun 4, 2015 at 15:13

4 Answers 4

8

No, it works on strings, or single dimension arrays.... you could use it through the callback in an array_walk_recursive though

array_walk_recursive(
    $myarray,
    function (&$value) {
        $value = str_replace('.my', '', $value);
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure, but as I read the question he only wants to replace this on the key "value4": out of the value4 field of these arrays. But maybe OP has to write this clearer.
4

You can use array_walk_recursive() http://php.net/manual/en/function.array-walk-recursive.php to perform a replacement on every sub-element (Note: Callback is just triggered for leafs (non-arrays)):

$myArray = array(0 => "my.test", 2=> array("test" => "my.thing"));

array_walk_recursive($myArray, "removeMy");

function removeMy(&$element, $index){
   $element = str_replace("my.", "", $element);
}


print_r($myArray); // Array ( [0] => test [2] => Array ( [test] => thing ) ) 

if the replacement should only appear on value4 keys - add that as a condition:

function removeMy(&$element, $index){
  if ($index === "value4"){
       $element = str_replace("my.", "", $element);
  }
}

3 Comments

This ultimately ended up working for me. Only question is how would I write that changed value back to the array? This doesn't seem to do so.
EDIT: Sorry, I typed your answer instead of copy and I missed the "&" before the $value. Thanks! So the "&" makes it write to the source array
@n1nja The & in front of a variable (for method input parameters) makes the variable containing just a reference on the original variable. Using $element without & will create a exact copy which you then can modify without changing the original array.
2

Make it easy

foreach ($array as &$item)
   $item['value4'] = str_replace('my.', "", $item['value4']);

Comments

0

I agree with @splash58, there is no need for a recursive approach for your 2d array.

You can most directly access and mutate the specific column value using a foreach(). Declaring &$v in the loop signature ensures that any changes to that value are represented in the original array.

Code: (Demo)

foreach ($array as ['value4' => &$v]) {
    $v = str_replace("my.", "", $v);
}
var_export($array);

Of course the same can be done with whole rows, but your sample data indicates that only one column is meaningfully targeted. Anyhow, here is how you can use str_replace() to modify 1d arrays in the same fashion: (Demo)

foreach ($array as &$row) {
    $row = str_replace("my.", "", $row);
}
var_export($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.