Array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Array question

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post 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);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

alter the function to

Code: Select all

return floor($val / 10);
Post Reply