How to remove parentheses of values in an array?

Asked

Viewed 402 times

7

I have an array and need to remove all parentheses from all values array. Follows the array:

$array = array(
    "chave1" => "(valor1)",
    "chave2" => "(valor2)",
    "chave3" => "(valor3)"
);

I need you to stay like this:

$array = array(
    "chave1" => "valor1",
    "chave2" => "valor2",
    "chave3" => "valor3"
);

How can I do this using php?

4 answers

10


Can use array_map(), to apply an anonymous function to each item in the array. The substitution is on account of the str_replace() looking for ( and ) and swap for nothing. A new array is generated:

$array = array(
        "chave1" => "(valor1)",
        "chave2" => "(valor2)",
        "chave3" => "(valor3)"
);

$novo = array_map(function($item){return str_replace(array('(', ')'), array('', ''), $item);}, $array);

echo "<pre>";
print_r($n);

Another option is to use array_walk() he does almost the same thing array_map() but does not generate a new array, as the argument is passed by reference the change is made in the element itself.

array_walk($array, function(&$item){ $item = str_replace(array('(', ')'), array('', ''), $item);});

echo "<pre>";
print_r($array);
  • A doubt, in performance, the array_map overcomes a repeat structure? + 1, great solution.

  • @lvcs did not take any performance test, which I can say that array_map()/array_walk() can be more practical in some situations, avoiding the for/foreach.

  • @Ivcs All native functions (written in C) are superior in performance than any structure created with pure PHP.

  • @rray yes for sure is more practical :)

  • Worked like a charm!

  • a hint will be you always opt for PHP’s own methods, as in the case of array_map(), because they are optimized to work in the best way with the language.

Show 1 more comment

5

You can easily change using the function array_walk together with a good regular expression.

array_walk - Apply a user supplied Function to Every Member of an array

As the manual says, array_walk traverse all elements of an array by applying to each of them a function given as parameter.

To remove the parentheses, I will use a regular expression. A ER in question, it is simple \(([^()]+)\). Translating, it means "anything within parentheses other than parentheses".

As already informed, it is necessary to provide a function to array_walk. In this case, a function such as callback. The function is as follows:

$callback = function(&$value , $key) {
    preg_match('/\(([^()]+)\)/' , $value , $matches);

    $value = $matches[1];
};

Applying your array with the callback.

array_walk($array , $callback);

You will get the following result:

array(3) { ["chave1"]=> string(6) "valor1" ["chave2"]=> string(6) "valor2" ["chave3"]=> string(6) "valor3" }
  • 1

    Good solution, even more using regex :)

  • 1

    @Dichrist, thank you very much. With regex, you will only take what exists between parentheses. If there are external values, they will be ignored.

4

You can try like this:

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

I basically went through all elements of the array and switched ( and ) for nothing.

  • Worked here bro!

4

You can go through the array with a foreach loop, use str_replace to take the parentheses and write the items without the parentheses in a new array:

$parentheses = array('(', ')');
$newArray = [];
foreach ($array as $value) {
  $value = str_replace($parentheses, '', $value);
  $newArray[] = $value;
}
  • -1 because it won’t work, $value is a local variable of the repeating structure, when you leave the repeating structure and when you traverse another element of the array it ceases to exist, then it’s like you haven’t done anything in this repeating structure, the correct would be to use $array[$key], when change I withdraw the -1.

  • You’re right. I changed the answer to write a new array, so it’s not duplicated, because your answer already does what you commented ;)

  • 1

    Another way to solve the problem by pointing is to change the value by reference, just add a & before the ex variable: foreach ($array as &$value)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.