Array values?
Moderator: General Moderators
Array values?
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?
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
Solution: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
)
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
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.
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.
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.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.
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.
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.