How to treat each value in an array without knowing the index name?

1

This is the following, I am creating a function that receives an array and transforms the writing into uppercase, the code is as follows:

function uppercase($post_array) //ok
{
    $post_array['clienteNomeCompleto'] = mb_strtoupper($post_array['clienteNomeCompleto']);

    return $post_array;
}

Well that's the problem, in this example, I know the value is in the fieldCompleteName field, but the other fields I do not know the name, because the same function will receive data from 3 different lists, one from clients, another of suppliers, and another one of employees, each with specific fields, hence I wanted to know, how do I do this without having to specify the field name? I tried it like this:

function uppercase($post_array)
{
    $post_array = mb_strtoupper($post_array);
    return $post_array;
}

And so:

function uppercase($post_array) //ok
{
    $count = count($post_array)
   for( $i=0; i<=$count; $i++){
   mb_strtoupper($post_array[$i]);
   }
    return $post_array;
}

Even with a foreach I tried:

    function uppercase($post_array) //ok
{
    foreach ($post_array as $value){
    $value = mb_strtoupper($value);
    }
    return $value;
}

Can anyone give me a light, what am I doing wrong? And if it is not uncomfortable, as I do the same function, except that to remove the characters,.;: | \ | -_ () [] {} too.

    
asked by anonymous 25.04.2017 / 04:40

1 answer

1

Simple, use array_map . It maps each item of array by applying the desired function:

$post_array = array_map('mb_strtoupper', $dados);

The form you tried with foreach would work fine if you used reference in value.

function array_to_upper_case(array $post_array) {

    foreach ($post_array as &$value) {
        $value = mb_strtoupper($value);
    }

    return $post_array;
}
    
25.04.2017 / 04:44