Avatar of Solutionabc
Solutionabc

asked on 

find the amount of different values in array

Hi,

I have an array that I would like to count the amount of different values that it holds. for example.

$people[1];
$people[2];
$people[1];
$people[4];

I would like to return 3 because that is the amount of unique numbers in the array.

also, I would like to do the same thing with an array that holds timestamps in the form of 2011-02-05 13:58:23 - i'm interested in finding the amount of unique stamps

thanks for the help.

PHPMySQL Server

Avatar of undefined
Last Comment
Ray Paseur
ASKER CERTIFIED SOLUTION
Avatar of Erdinç Güngör Çorbacı
Erdinç Güngör Çorbacı
Flag of Türkiye image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

@Solutionabc: It is usually a good idea to test any code before you accept it as a solution to your question.  In this case I could see that you had not tested it, so I made up a test case for you.  Please see the code snippet, copied from the accepted solution here.  You can see that I added error_reporting(E_ALL); to the script.  This is useful when you do not know what a script is doing with the data because you will get the Notice level messages.  The script is installed on my server here:
http://www.laprbass.com/RAY_temp_Solutionabc.php

Outputs:
Notice: Undefined variable: people in /home/websitet/public_html/RAY_temp_Solutionabc.php on line 5
Notice: Undefined variable: people in /home/websitet/public_html/RAY_temp_Solutionabc.php on line 6
Notice: Undefined variable: people in /home/websitet/public_html/RAY_temp_Solutionabc.php on line 7
Notice: Undefined variable: people in /home/websitet/public_html/RAY_temp_Solutionabc.php on line 8
Notice: Undefined variable: people in /home/websitet/public_html/RAY_temp_Solutionabc.php on line 10
Warning: array_unique() expects parameter 1 to be array, null given in /home/websitet/public_html/RAY_temp_Solutionabc.php on line 10
0

I'll ask a moderator to reopen the question.  Best regards, ~Ray
<?php // RAY_temp_Solutionabc.php
error_reporting(E_ALL);

// CODE FROM EE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27047301.html?cid=1135#a35790573
$people[1];
$people[2];
$people[1];
$people[4];

$array_with_unique_values=array_unique($people);
echo count($array_with_unique_values);

Open in new window

Surely an array definition and usage is not done like the asker's faulty syntax.
But the question here was not "how an array should be defined and populated". I think the accepted answer for the asked question "find the amount of different values in array" is right. But additionally comment #35796914 informs asker about a faulty array assignment which is absolutely related with the question. So i also agree comment #35796914 should be an assisted answer.
I think the both answers are working with a right constructed array. And the question is not building arrays but eliminating same values in an array for counting. So answer in first comment is a solution to the question. But the asker's example has a wrong array definition and after correcting that the complete answer to example is shaped as below ;

 
$people[]=1;
$people[]=2;
$people[]=1;
$people[]=4;

if(isset($) && !empty($people)){
array_with_unique_values = array();
$array_with_unique_values=array_unique($people);
echo count($array_with_unique_values);
}

Open in new window


However, if we start examining last 10 days accepted answers with error_reporting(E_ALL); directive, i'm sure more than %50 of the accepted answers will have some errors. So we are not answering to the all structure or logic. Just the asked question in an example.

If we don't ; We will need to talk about every line of code soo much. We had to assume the asker knows (or don't need to know) the other thing that could be talked about. We just serve the problem's logical solution.

Anyway , this problem and example code's the only full solution is included in this comment.

Best Regards
Avatar of Solutionabc
Solutionabc

ASKER

apologies for using sloppy code to depict my question. The first response seemed to work for me when I plugged it in. I was assuming people would focus more on what I was asking rather than the way I asked it.

i'll keep this in mind for next time,
sorry for any inconvenience.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I was assuming people would focus more on what I was asking rather than the way I asked it. - we usually have to focus on both to understand the issues.

For better or worse, programming is a fairly precise activity, and while we have great latitude in describing the meaning of ideas in natural language, when it comes to putting those ideas into program code, we are constrained by rigid rules of data and instruction definition.  For that reason, and also to ensure that I do not waste anyone's time with a useless example, I test my code before I post it in a code snippet here at EE.  Or else, if I cannot test because I do not have a test data set, I try to make it clear that I have posted untested code.

That doesn't mean my answers are perfect - but it helps.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo