By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

array_count_values

P: n/a
hello,

here is the code:

<?
$a=array(1,3,4,5,6,7,8,9,10);
$b=array_count_values($a);
print_r($b);
?>

the result will be: [1]=>1 [3]=>1...........

I would like that the keys which doesnt appear in the result of
array_count_values (here it is the number 2)
have the value 0.......the new result will be: [1]=>1 [2]=>0
[3]=>1...........

how can we formulate a php code to resolve that ?
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
<a> wrote:
I would like that the keys which doesnt appear in the result of
array_count_values (here it is the number 2)
have the value 0.......the new result will be: [1]=>1 [2]=>0
[3]=>1...........

how can we formulate a php code to resolve that ?

php$ cat countall.php
<?php
$a = array(1, 3, 4, 5, 6, 7, 8, 9, 10);
$b = array_count_values($a);

#print_r($b);

for ($i = 0; $i <= max($a); ++$i) {
if (!isset($b[$i])) $b[$i] = 0;
}
ksort($b);
print_r($b);
?>

php$ php countall.php
Array
(
[0] => 0
[1] => 1
[2] => 0
[3] => 1
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => 1
)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2

P: n/a
a wrote:
hello,

here is the code:

<?
$a=array(1,3,4,5,6,7,8,9,10);
$b=array_count_values($a);
print_r($b);
?>

the result will be: [1]=>1 [3]=>1...........

I would like that the keys which doesnt appear in the result of
array_count_values (here it is the number 2)
have the value 0.......the new result will be: [1]=>1 [2]=>0
[3]=>1...........

how can we formulate a php code to resolve that ?


Like this, for example:

$a = array(1,3,4,5,6,7,8,9,10);
$range = range(1,10);
foreach($range as $number) {
if (in_array($number, $a)) {
$b[$number] = 1;
} else {
$b[$number] = 0;
}
}

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #3

P: n/a
Pedro Graca wrote:
<?php
$a = array(1, 3, 4, 5, 6, 7, 8, 9, 10);
$b = array_count_values($a);

#print_r($b);

for ($i = 0; $i <= max($a); ++$i) {
Make that

for ($i = 1; $i < max($a); ++$i) {
if (!isset($b[$i])) $b[$i] = 0;
}
ksort($b);
print_r($b);
?>


--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #4

P: n/a
I think the below works.

Scott
$a=array(1,3,4,5,6,7,8,9,10);
$b=array_count_values($a);

//sort the key
ksort($b);

//document highest key
foreach($b as $key=>$value)
{
$counter=$key;
}

//load elements that do not exist with zeros
for($i=$counter;$i>=0;$i--)
{
if(!isset($b[$i]))
{
$b[$i]=0;
}
}

//sort by key again
ksort($b);
print_r($b);


<a> wrote:
hello,

here is the code:

<?
$a=array(1,3,4,5,6,7,8,9,10);
$b=array_count_values($a);
print_r($b);
?>

the result will be: [1]=>1 [3]=>1...........

I would like that the keys which doesnt appear in the result of
array_count_values (here it is the number 2)
have the value 0.......the new result will be: [1]=>1 [2]=>0
[3]=>1...........

how can we formulate a php code to resolve that ?


Jul 17 '05 #5

P: n/a
Pedro's is more concise.

Scotty

Jul 17 '05 #6

P: n/a
Pedro's is more concise.

Scotty

Jul 17 '05 #7

P: n/a
a wrote:
I would like that the keys which doesnt appear in the result of
array_count_values (here it is the number 2)
have the value 0.......the new result will be: [1]=>1 [2]=>0
[3]=>1...........

how can we formulate a php code to resolve that ?


<?php

$a = array(1,3,4,5,6,7,8,9,10);
$b = array_count_values($a);
$c = array_fill(1,10,0);
$d = array_diff($c,$b);
ksort($e = $b + $d);
print_r($e);

?>
JW

Jul 17 '05 #8

P: n/a
Tank you for all these answers !
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.