Re: Replace punctuation in an associative array

Arno Welzel <[email protected]> Fri, 3 Mar 2023 08:14:22 +0100
Newsgroups comp.lang.php
Message-ID <[email protected]>
The Doctor, 2023-03-02 13:44:

> I wish to replace [[ with [
> 
> AND
> 
> ]] with ] in an associative array .
> 
> How can I do this?

What do you mean with "in an associative array"? The values? The keys?

If you want to replace the characters in values:

<?php
$array = [
  'value 1' => '[[something 1',
  'value 2' => 'something 2]]',
];

// Output array as it was before
print_r($array);

foreach($array as $key => $value)
{
  $array[$key] = str_replace(
    [ '[[', ']]' ],
    [ '[', ']' ],
    $value
  );
}

// Output as it was afterwards
print_r($array);



-- 
Arno Welzel
https://arnowelzel.de