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
Array question
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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);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);
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);
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
alter the function to
Code: Select all
return floor($val / 10);