Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7192 > unrolled thread

Count how many times a value occurs in an array

Started byRyan <rbilesky@gmail.com>
First post2012-09-26 23:25 -0700
Last post2012-09-28 17:40 +0200
Articles 15 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  Count how many times a value occurs in an array Ryan <rbilesky@gmail.com> - 2012-09-26 23:25 -0700
    Re: Count how many times a value occurs in an array aroniss@gmail.com - 2012-09-27 00:10 -0700
    Re: Count how many times a value occurs in an array Captain Paralytic <paul_lautman@yahoo.com> - 2012-09-27 03:48 -0700
      Re: Count how many times a value occurs in an array Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-09-27 13:08 +0200
        Re: Count how many times a value occurs in an array "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-09-27 18:19 +0200
          Re: Count how many times a value occurs in an array Ryan <rbilesky@gmail.com> - 2012-09-27 10:04 -0700
          Re: Count how many times a value occurs in an array Ryan <rbilesky@gmail.com> - 2012-09-27 10:14 -0700
            Re: Count how many times a value occurs in an array "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-09-27 19:43 +0200
              Re: Count how many times a value occurs in an array Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 20:07 +0200
                Re: Count how many times a value occurs in an array "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-09-27 23:52 +0200
                  Re: Count how many times a value occurs in an array Christoph Becker <cmbecker69@gmx.de> - 2012-09-28 00:07 +0200
                    array_walk() callback Was: Count how many times a value occurs in an array "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-09-28 10:04 +0200
            Re: Count how many times a value occurs in an array Jerry Stuckle <jstucklex@attglobal.net> - 2012-09-27 18:26 -0400
        Re: Count how many times a value occurs in an array Captain Paralytic <paul_lautman@yahoo.com> - 2012-09-28 07:35 -0700
          Re: Count how many times a value occurs in an array Christoph Becker <cmbecker69@gmx.de> - 2012-09-28 17:40 +0200

#7192 — Count how many times a value occurs in an array

FromRyan <rbilesky@gmail.com>
Date2012-09-26 23:25 -0700
SubjectCount how many times a value occurs in an array
Message-ID<475e0b6d-5e6e-4a60-9ec8-21f068bc3913@googlegroups.com>
I have an array lets say for example:

$my_array = array('1', '1', '2', '3', '1', '3', '2');

I want a listing of each value in the array and how many times it occurs for in this case it would look like this

array ('1' => 3, '2' => 2, '3' => 2)

jw if ph has a build in function to make this easy of am I going to have to roll my own by looping though the array?

[toc] | [next] | [standalone]


#7193

Fromaroniss@gmail.com
Date2012-09-27 00:10 -0700
Message-ID<86290b83-df8d-46d6-9f37-3853ce015776@googlegroups.com>
In reply to#7192
On Thursday, September 27, 2012 8:25:21 AM UTC+2, Ryan wrote:
> I have an array lets say for example:
> 
> 
> 
> $my_array = array('1', '1', '2', '3', '1', '3', '2');
> 
> 
> 
> I want a listing of each value in the array and how many times it occurs for in this case it would look like this
> 
> 
> 
> array ('1' => 3, '2' => 2, '3' => 2)
> 
> 
> 
> jw if ph has a build in function to make this easy of am I going to have to roll my own by looping though the array?

Use built-in function: array_count_values($my_array)

[toc] | [prev] | [next] | [standalone]


#7194

FromCaptain Paralytic <paul_lautman@yahoo.com>
Date2012-09-27 03:48 -0700
Message-ID<c8b9fe91-9d7b-4918-b910-835c50399743@h4g2000yqo.googlegroups.com>
In reply to#7192
On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
>
> jw if ph has a build in function to make this easy of am I going to have to roll my own by looping though the array?

Care to explain what the above is supposed to mean?

[toc] | [prev] | [next] | [standalone]


#7195

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-09-27 13:08 +0200
Message-ID<5064339a$0$6841$e4fe514c@news2.news.xs4all.nl>
In reply to#7194
On 9/27/2012 12:48 PM, Captain Paralytic wrote:
> On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
>>
>> jw if ph has a build in function to make this easy of am I going to have to roll my own by looping though the array?
>
> Care to explain what the above is supposed to mean?
>

jw probably means: Just wondering
ph probably means PHP

The remainder is reasonably clear (I think).

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#7197

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-09-27 18:19 +0200
Message-ID<acjcjqF9iovU1@mid.uni-berlin.de>
In reply to#7195
Am 27.09.2012 13:08, schrieb Erwin Moller:
> On 9/27/2012 12:48 PM, Captain Paralytic wrote:
>> On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
>>>
>>> jw if ph has a build in function to make this easy of am I going to have to roll
>>> my own by looping though the array?
>>
>> Care to explain what the above is supposed to mean?
>>
> 
> jw probably means: Just wondering
> ph probably means PHP
> 

I hope (for him) the OP does not program the same way he is writing.

/Str.

[toc] | [prev] | [next] | [standalone]


#7198

FromRyan <rbilesky@gmail.com>
Date2012-09-27 10:04 -0700
Message-ID<5fb2ea84-15bc-49ef-ae87-8534426cd45d@googlegroups.com>
In reply to#7197
On Thursday, September 27, 2012 9:19:08 AM UTC-7, M. Strobel wrote:
> Am 27.09.2012 13:08, schrieb Erwin Moller:
> 
> > On 9/27/2012 12:48 PM, Captain Paralytic wrote:
> 
> >> On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
> 
> >>>
> 
> >>> jw if ph has a build in function to make this easy of am I going to have to roll
> 
> >>> my own by looping though the array?
> 
> >>
> 
> >> Care to explain what the above is supposed to mean?
> 
> >>
> 
> > 
> 
> > jw probably means: Just wondering
> 
> > ph probably means PHP
> 
> > 
> 
> 
> 
> I hope (for him) the OP does not program the same way he is writing.
> 
> 
> 
> /Str.

no will rray_count_values() not work lol. It was late when I wote it. :/

[toc] | [prev] | [next] | [standalone]


#7199

FromRyan <rbilesky@gmail.com>
Date2012-09-27 10:14 -0700
Message-ID<42505440-8980-4772-85d5-303998824b62@googlegroups.com>
In reply to#7197
On Thursday, September 27, 2012 9:19:08 AM UTC-7, M. Strobel wrote:
> Am 27.09.2012 13:08, schrieb Erwin Moller:
> 
> > On 9/27/2012 12:48 PM, Captain Paralytic wrote:
> 
> >> On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
> 
> >>>
> 
> >>> jw if ph has a build in function to make this easy of am I going to have to roll
> 
> >>> my own by looping though the array?
> 
> >>
> 
> >> Care to explain what the above is supposed to mean?
> 
> >>
> 
> > 
> 
> > jw probably means: Just wondering
> 
> > ph probably means PHP
> 
> > 
> 
> 
> 
> I hope (for him) the OP does not program the same way he is writing.
> 
> 
> 
> /Str.

Oh so will arra_count_values not work? lol that's what I get for typing tired. Fortunately for me my IDE will highlight any errors lol.

On a more serious note though I have noticed one limitation. It will no go into sub arrays. So when I query my DB it will actually come up like this

array (array(1), array(1), array (2)) ect. So what would be the best way to deal with this, is there a way to go though and clean it up by removing the nested arrays and replacing them with integers or even a literal string would work for me.

I suppose if all else fails I could just loop though the primary array and implode each sub array.

[toc] | [prev] | [next] | [standalone]


#7201

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-09-27 19:43 +0200
Message-ID<acjhinFap9pU1@mid.uni-berlin.de>
In reply to#7199
Am 27.09.2012 19:14, schrieb Ryan:
> On Thursday, September 27, 2012 9:19:08 AM UTC-7, M. Strobel wrote:
>> Am 27.09.2012 13:08, schrieb Erwin Moller:
>>
>>> On 9/27/2012 12:48 PM, Captain Paralytic wrote:
>>
>>>> On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
>>
>>>>>
>>
>>>>> jw if ph has a build in function to make this easy of am I going to have to roll
>>
>>>>> my own by looping though the array?
>>
>>>>
>>
>>>> Care to explain what the above is supposed to mean?
>>
>>>>
>>
>>>
>>
>>> jw probably means: Just wondering
>>
>>> ph probably means PHP
>>
>>>
>>
>>
>>
>> I hope (for him) the OP does not program the same way he is writing.
>>
>>
>>
>> /Str.
> 
> Oh so will arra_count_values not work? lol that's what I get for typing tired. Fortunately for me my IDE will highlight any errors lol.
> 
> On a more serious note though I have noticed one limitation. It will no go into sub arrays. So when I query my DB it will actually come up like this
> 
> array (array(1), array(1), array (2)) ect. So what would be the best way to deal with this, is there a way to go though and clean it up by removing the nested arrays and replacing them with integers or even a literal string would work for me.
> 
> I suppose if all else fails I could just loop though the primary array and implode each sub array.

Yes you get an array of arrays from a database query.

PHP has a ton of functions... let's see... I would use array_walk_recursive().

Be careful with call-by-reference, it is not allowed in the newest PHP versions.

/Str.

[toc] | [prev] | [next] | [standalone]


#7202

FromChristoph Becker <cmbecker69@gmx.de>
Date2012-09-27 20:07 +0200
Message-ID<k424l7$fq5$1@speranza.aioe.org>
In reply to#7201
M. Strobel wrote:
> Be careful with call-by-reference, it is not allowed in the newest PHP versions.

Only the call-time call-by-reference was deprecated in 5.3 and removed
in 5.4.  Declaring a parameter of a function as reference is absolutely
fine.

E.g.:

function increase(&$n)
{
    $n++;
}

$i = 1; increase($i); // $i == 2
$i = 1; increase(&$i); // fatal error in PHP 5.4

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#7203

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-09-27 23:52 +0200
Message-ID<ack04eFe8fvU1@mid.uni-berlin.de>
In reply to#7202
Am 27.09.2012 20:07, schrieb Christoph Becker:
> M. Strobel wrote:
>> Be careful with call-by-reference, it is not allowed in the newest PHP versions.
> 
> Only the call-time call-by-reference was deprecated in 5.3 and removed
> in 5.4.  Declaring a parameter of a function as reference is absolutely
> fine.
> 
> E.g.:
> 
> function increase(&$n)
> {
>     $n++;
> }
> 
> $i = 1; increase($i); // $i == 2
> $i = 1; increase(&$i); // fatal error in PHP 5.4
> 

Yes, but I had a problem  with callback functions: if you created them with
create_function this was a call-time call-by-reference.


/Str.

[toc] | [prev] | [next] | [standalone]


#7204

FromChristoph Becker <cmbecker69@gmx.de>
Date2012-09-28 00:07 +0200
Message-ID<k42ime$k3j$1@speranza.aioe.org>
In reply to#7203
M. Strobel wrote:
> Yes, but I had a problem  with callback functions: if you created them with
> create_function this was a call-time call-by-reference.

Could you please give an example. For example

array_walk($array, create_function('&$x', '$x = trim($x);'));

works fine.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#7207 — array_walk() callback Was: Count how many times a value occurs in an array

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-09-28 10:04 +0200
Subjectarray_walk() callback Was: Count how many times a value occurs in an array
Message-ID<acl40oFlkvtU1@mid.uni-berlin.de>
In reply to#7204
Am 28.09.2012 00:07, schrieb Christoph Becker:
> M. Strobel wrote:
>> Yes, but I had a problem  with callback functions: if you created them with
>> create_function this was a call-time call-by-reference.
> 
> Could you please give an example. For example
> 
> array_walk($array, create_function('&$x', '$x = trim($x);'));
> 
> works fine.

My requirement was not to change the array, but to return data from inside the callback.

In older PHP you could ignore the warning about Call-time pass-by-reference and give
the callback function a third parameter by ref.

Now there is only one way to do it, see the command line test code below.

/Str.

<?php
/**
 * Testen von array_walk mit Closure und use-Parameter by reference
 */

error_reporting(-1);

$sub1 = array('I'=>'Igor', 'J'=>'Jan', 'K'=>'Karl', 'L'=>'Liugi', 'M'=>'Michael');
$sub2 = array('A'=>'Alexa', 'B'=>'Brunhilda','C'=>'Carla','D'=>'Dina','E'=>'Elsa');
$a['F'] = 'Frantzi';
$a['G'] = 'Gustavo';
$a['Gentlemen'] = $sub1;
$a['H'] = 'Hillbilly';
$a['Ladies'] = $sub2;

echo "Number of elements using count(): ", count($a), PHP_EOL;

echo (array_key_exists('K', $a)) ? 'array_key_exists() does work in
sub-arrays':'array_key_exists() does NOT work in sub-arrays', PHP_EOL;


echo '------------------------------',  PHP_EOL;
echo "\tStart run\n";
echo '------------------------------',  PHP_EOL;

/**
 * try a call by ref in a closure use list
 *
 * This is the best solution to return values from the callback.
*/
$f0 = function ($v,$k,$l) use(&$var) {
        if ($k==$l)  $var = $v;
};

$var = 'something';
echo "var before array_walk: ($var)\n";
# this does work, var will be defined: unset($var);
$rc = array_walk_recursive($a, $f0, 'M');

echo "rc = $rc, var after array_walk f0 with call by ref in use list: ($var)\n";
echo '------------------------------',  PHP_EOL;

/**
 * testing f0 in a different scope
 */
function func0($arr,$callback) {
    $var = 'Testing in a local scope with f0';
    echo "var before array_walk: ($var)\n";
    $rc = array_walk_recursive($arr, $callback, 'M');
    return ($rc) ? 'Call successful, var is '.$var : 'Call NOT successful, var is
'.$var ;
}

echo func0($a,$f0), PHP_EOL;
echo "Variable in global scope after call: $var\n";
echo "Result: the var by ref in the use list binds to the global scope where it was
defined.\n";

/**
 * trying to put a call by ref into the parameter list
 */

$f1 =  function ($v,$k,$l, &$var)  {
        if ($k==$l)  $var = $v;
};

$var = 'Test Closure f1';
echo "var before array_walk: ($var)\n";
# this does work, var will be defined: unset($var);
$rc = array_walk_recursive($a, $f1, 'L', $var);

echo "rc = $rc, var after array_walk f1 with call by ref in parameter list: ($var)\n";
echo "We can only pass 1 extra parameter into the callback, f1 does not work\n";
echo '------------------------------',  PHP_EOL;

/**
 * the idea is to test if we have call by ref in a closure parameter
 */
$f2 =  function ($v,$k, &$var)  {
        if ($k=='I')  $var = $v;
};

$var = 'Test Closure f2';
echo "var before array_walk: ($var)\n";
$rc = array_walk_recursive($a, $f2, $var);

echo "rc = $rc, var after array_walk f2 with call by ref in parameter list: ($var)\n";
#echo "We can only pass 1 extra parameter into the callback, f1 does not work\n";
echo '------------------------------',  PHP_EOL;

echo <<<EOD
Result for Closure as callback in array_walk:

We can pass variables by ref in the use() parameter list, and only there.
This is the way to return results.

The callback invocation supports one more variable or value, after value and key.

EOD
;

[toc] | [prev] | [next] | [standalone]


#7205

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-09-27 18:26 -0400
Message-ID<k42jr8$8go$1@dont-email.me>
In reply to#7199
On 9/27/2012 1:14 PM, Ryan wrote:
> On Thursday, September 27, 2012 9:19:08 AM UTC-7, M. Strobel wrote:
>> Am 27.09.2012 13:08, schrieb Erwin Moller:
>>
>>> On 9/27/2012 12:48 PM, Captain Paralytic wrote:
>>
>>>> On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
>>
>>>>>
>>
>>>>> jw if ph has a build in function to make this easy of am I going to have to roll
>>
>>>>> my own by looping though the array?
>>
>>>>
>>
>>>> Care to explain what the above is supposed to mean?
>>
>>>>
>>
>>>
>>
>>> jw probably means: Just wondering
>>
>>> ph probably means PHP
>>
>>>
>>
>>
>>
>> I hope (for him) the OP does not program the same way he is writing.
>>
>>
>>
>> /Str.
>
> Oh so will arra_count_values not work? lol that's what I get for typing tired. Fortunately for me my IDE will highlight any errors lol.
>
> On a more serious note though I have noticed one limitation. It will no go into sub arrays. So when I query my DB it will actually come up like this
>
> array (array(1), array(1), array (2)) ect. So what would be the best way to deal with this, is there a way to go though and clean it up by removing the nested arrays and replacing them with integers or even a literal string would work for me.
>
> I suppose if all else fails I could just loop though the primary array and implode each sub array.
>

If it's coming from a database, just let the database gives you the counts.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#7208

FromCaptain Paralytic <paul_lautman@yahoo.com>
Date2012-09-28 07:35 -0700
Message-ID<4cd86ca2-d555-4873-b65c-0b0b907b17ed@p5g2000vbl.googlegroups.com>
In reply to#7195
On Sep 27, 12:08 pm, Erwin Moller <erwinmolleruse...@xs4all.nl> wrote:
> On 9/27/2012 12:48 PM, Captain Paralytic wrote:
>
> > On Sep 27, 7:25 am, Ryan <rbile...@gmail.com> wrote:
>
> >> jw if ph has a build in function to make this easy of am I going to have to roll my own by looping though the array?
>
> > Care to explain what the above is supposed to mean?
>
> jw probably means: Just wondering
> ph probably means PHP
>
> The remainder is reasonably clear (I think).
Those first 2 weren't what I was having particular trouble with. The
rest of it is total nonsense!

[toc] | [prev] | [next] | [standalone]


#7209

FromChristoph Becker <cmbecker69@gmx.de>
Date2012-09-28 17:40 +0200
Message-ID<k44gcj$16i$1@speranza.aioe.org>
In reply to#7208
Captain Paralytic wrote:
> Those first 2 weren't what I was having particular trouble with. The
> rest of it is total nonsense!

I suppose, what was meant is:

Just wondering, if PHP has a built in function to make this easy, or do
I have to roll my own (by looping through the array)?

-- 
Christoph M. Becker

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web