Page 1 of 1

Array question

Posted: Mon Mar 06, 2006 9:55 am
by pedroz
I have the following array:

$string = ('10','21','22','30','41','42','43');

I need to count the number of values which start with 1, 2, 3, 4

to get this...
1: result 1
2: result 2
3: result 1
4: result 3

Posted: Mon Mar 06, 2006 10:01 am
by feyd

Code: Select all

function firstChar($val)
{
  return $val{0};
}

$temp = array_map('firstChar', $string);
var_dump($temp);
$temp = array_count_values($temp);
var_dump($temp);

Posted: Mon Mar 06, 2006 10:31 am
by pedroz
Hi feyd,

Just another 2 points to be completed:

1.
$string = ('10','21','22','30','41','42','43','50','60','70','80','90','100','101','102','103','104','105','106','107','108','109','110');
1: result 1
2: result 2
3: result 1
4: result 3
5:6:7:8:9: result 1
10: result 11

2.
put result in array
$result=(1,2,1,3,1,1,1,1,1,2,11);

Posted: Mon Mar 06, 2006 10:40 am
by feyd
alter the function to

Code: Select all

return floor($val / 10);