Array values?

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
Telos
Forum Commoner
Posts: 37
Joined: Sat Aug 02, 2003 9:03 am
Location: Finland

Array values?

Post by Telos »

ok, I need to be able to find out the values in an array which appear only certain times in it. For example I have a array which has the following values

1, 1, 2, 3, 3, 3, 4, 4, 5

and then I would like to know the values which appear ie. 3 times in array and I would get only 3 as a result. So how do I do this?
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

i would run over the array, and foreach array value increment the value of another array
$couterarray['value']=$count_of_appearance
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

php manual:

array_count_values
Telos
Forum Commoner
Posts: 37
Joined: Sat Aug 02, 2003 9:03 am
Location: Finland

Post by Telos »

hmm...I tried it but I just can't get it to work. I have:
$vastaaja_array as the array which has all those values inside. So maybe you could be nice enough to explain this with the code to me.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

see user notes in the manual, i.e. RTM
Telos
Forum Commoner
Posts: 37
Joined: Sat Aug 02, 2003 9:03 am
Location: Finland

Post by Telos »

No use in user notes, have checked already and couldn't find anything usefull there.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

php manual wrote: Example 91. array_count_values() example

$array = array (1, "hello", 1, "world", "hello");
print_r(array_count_values ($array));

The printout of the above program will be:

Array
(
[1] => 2
[hello] => 2
[world] => 1
)
Solution:

Code: Select all

$myResultArray=array_count_values($myArray);
foreach($myResultArray as $key=>$value)
   echo "$key exists $value times.";
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

this is exactly what you need.

if you want to get the value that were 3 times in your array just do this:
$result=array_count_values($vastaaja_array);
foreach ($result as $key => $val) {
if ($val==3) echo "<br>This was 3 times in my array: ".$key;
}
Telos
Forum Commoner
Posts: 37
Joined: Sat Aug 02, 2003 9:03 am
Location: Finland

Post by Telos »

Thank you so much! it worked! and now I can go home from work feeling happy when I got it work :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I don't want to come across as being flippant or seem arrogant, but the answer to your question is in the manual as quoted above.
All you would have needed to do was use a simple foreach on the result array.

Everyone on this board takes some of their own time to answer questions, we are all volunteers, we don't get paid. Meaning: we are actually a nice bunch of people who like to help each other with problems that may arise when coding PHP.

BUT: the PHP manual is the a resource, it does not exist to solve your problems. It simply exists to describe how to make use of PHP's abilities, it's functions etc.
Meaning: before you consult any resource, know what you want, think of possible solutions beforehand. Then find a hint or part of the solution, see how it fits into what you thought of beforehand and, if necessary, rinse and repeat. This is also true for the this board.

Why you may ask? Three reasons:
1. It's your mind - use it.
2. It's your mind - use it.
3. It's your mind - use it.

There is a sticky regarding how to use this forum. Read it and think about it.
Telos
Forum Commoner
Posts: 37
Joined: Sat Aug 02, 2003 9:03 am
Location: Finland

Post by Telos »

Well I've been strugling with this problem for a month now and I had a software deadline next tuesday. I've been browsing google, php manual, every possible forum or resource site for 8-10 hours a day for a month without success. Maybe I was thinking too hard and too long for the answer since I never thought of that and to top of that I'm very new to working with arrays and I usually just try to avoid them so I think I had the right to post here this question and luckily I did and Tubbietoeter was kind enough to tell me the answer.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i would have said to buy oreilly's learning perl book and modify their way of finding outthe amount of instances in a heap for php, and learn a great scripting language while you're at it
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Telos wrote:Well I've been strugling with this problem for a month now and I had a software deadline next tuesday. I've been browsing google, php manual, every possible forum or resource site for 8-10 hours a day for a month without success. Maybe I was thinking too hard and too long for the answer since I never thought of that and to top of that I'm very new to working with arrays and I usually just try to avoid them so I think I had the right to post here this question and luckily I did and Tubbietoeter was kind enough to tell me the answer.
Like everyone else you and I have the right to post a question on this forum. I wrote my piece because you could have easily found the answer yourself, as I repeatedly pointed you towards the relevant section of the PHP manual.

If you had said: sorry, I don't understand arrays, I don't understand what this function you linked to does, it would have been a different story.
The way you came across repeatedly was simply: here's my problem, solve it. What you lacked in my eyes was the effort to employ your mind while expecting others to do so.
Telos
Forum Commoner
Posts: 37
Joined: Sat Aug 02, 2003 9:03 am
Location: Finland

Post by Telos »

yes, now that I look into your earlier post where you explain the array_count_values function I understand it. And yes, I haven't worked much at all with arrays so I'm a bit confused with the array functions still. Though I thought the array_count_values function myself too but just didn't know how to use it. Like I said I've been strugling with this problem for weeks now. Every day so I know I could've put more effort into this myself but I was just too tired to think it anymore. Usually when a problem comes I try to search google for an answer, then the php manual is very handy tool and then from IRC channels and if everything else fails I write the problem to forums. Usually at this point I'm already very frustrated to this problem :) But thank you from everyone's help. The main thing is that it works and the thanx goes to all of you who replied to this thread.
Post Reply