Skip to content
WJunction - Webmaster Forum

Count a function true/false

Status
Not open for further replies.
PHP:
foreach($filenames as $count){
$filedup=basename($count,'.txt');
$filedup=array($filedup);
print_r (array_count_values($filedup));
}

But this gives
Code:
Array ( [entry090914-020018] => 1 ) Array ( [entry091129-053238] => 1 ) Array ( [entry100716-184512] => 1 ) Array ( [entry100718-111228] => 1 )

So the array_count_values function does not work because everything is in separate array and that seems to be from foreach.
How can i do this in one array only ?
Example:
Code:
Array ( [entry090914-020018] => 1 ,[entry091129-053238] => 1 ,[entry100716-184512] => 1 ,[entry100718-111228] => 1 )
 

6 comments

if I did understan what you mean this code is what you want ::
PHP:
$bla=array_unique($array);
if isset($bla)
$count=count($array)-count($bla)
else
$count=0;
 
What i am trying to do is to count all duplicates in array and show them as number.
Eg:
array('fish','dog','pants','male','fish','fish','male')

I need to echo 3 fishes and 2 males

Note : I don't know the array content,neither the searching string.The function has to search for duplicates between the array values.
 
you can probably use in_array() and search each spot using a for loop

for($i = 0; $i < $arrayMax; $i++){
$total = 0;
if(in_array($array[$i]), $array){
$total = $total+1;
}
}

Somewhere along those lines.

EDIT: nevermind, just read your note, lol.


EDIT 2.0: was looking online, found something that may help.


Code:
<?php 
function arrayDuplicate($array) 
{ 
return array_unique(array_diff_assoc($array1,array_unique($array1))); 
}; 
?> 
 
Example: 
<?php 
$arr1 = array('foo', 'bar', 'xyzzy', '&', 'xyzzy', 
'baz', 'bat', '|', 'xyzzy', 'plugh', 
'xyzzy', 'foobar', '|', 'plonk', 'xyzzy', 
'apples', '&', 'xyzzy', 'oranges', 'xyzzy', 
'pears','foobar'); 
 
$result=arrayDuplicate($arr1); 
print_r($result);exit; 
?> 
 
Output: 
 
Array 
( 
[4] => xyzzy 
[12] => | 
[16] => & 
[21] => foobar 
)

Using this it will tell you the location of the duplicates, using this you can count them up each time using $total or something
 
PHP:
function arrayDuplicates($array)
{
    $check = array(); //Temporary 
    foreach($array as $item)
    {
        isset($check[$item]) ? ($check[$item]++) : ($check[$item] = 1);
    }
    return $check;
}
 
tnx litewarez but the problem is that the stupid foreach function adds each value to separate array as i wrote above.So the function is useless if it is not in one array.
 
Status
Not open for further replies.

About the author

Porsche_maniak
Active Member · Joined
Auto racing,Porsche,Web Development
283
Messages
0
Reactions
16
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom