How do i truncate array keys
Hey there, i am trying to generate a chart based on some array, but to assign the x labels, i need to truncate my keys to 5 characters or something...
so from :
Array ( [Fruits totals] => 61 [Vegetables totals] =&g开发者_开发技巧t; 71)
to
Array ( [Fruit] => 61 [Veget] => 71)
thanks in advance.
Just iterate over the array:
$truncated = array();
foreach($array as $key => $val) {
$truncated[substr($key, 0, 5)] = $val;
}
Note that two different entries in the original array could have the same truncated key. In this case the last entry will overwrite previous values.
精彩评论