Search code examples
phparraystruncate

Truncate all values in a flat array to n characters


How can I limit the character length of all string inside an array?

Like chunk_split ( $string, 10 );

$array = array(
    "foo" => "bar",
    "bar" => "foofoofoofoofoofoofoo",
    "name" => "name34234242224"
);

I do not want more than 10 characters in any string.


Solution

  • Like this:

    foreach($array as $key => $value){
        $array[$key] = substr($value,0,10);
    }