Jump to content

[SOLVED] Anybody want to learn the array_count_values() php function HERE U GO.....


redarrow

Recommended Posts

 

 

I had to learn this array function if a ip was twice in the database, i can tell the user that there download was not allowed and they have download twice.....

 

 

Anybody want to learn the array_count_values() php function HERE U GO.....

 

 

<?php

//array way to find x amount off people using array_count values() php function...

$x=array("john","lucky","john","petter","luke","petter","brian","petter");

$count=array_count_values($x);

$name=array("john","petter");

if($count[$name[0]] ==2 && $count[$name[1]]==3){

echo "{$name[0]} is there {$count[$name[0]]}times! and {$name[1]} is there {$count[$name[1]]} times! <br><br>";

print_r($count);
}



echo"<br><br>";

//varable way to find x amount off people using array_count values() php function...


$x=array("john","lucky","john","petter","luke","petter","brian","petter");

$count=array_count_values($x);

$name1="john";
$name2="petter";

if($count[$name1] ==2 && $count[$name2]==3){

echo "{$name1} is there {$count[$name1]}times! and {$name2} is there {$count[$name2]} times! <br><br>";

print_r($count);
}

?>

according to everythink i have read php 6 will allow the [] and the {} to be used to echo statement out is it correct...

 

 

php above version 6 [] not allowed

<?php
echo "{$name1} is there {$count[$name1]}times! and {$name2} is there {$count[$name2]} times! <br><br>";
?>

 

php version 6 both ways allowed

<?php
echo "{$name1} is there {$count[$name1]}times! and {$name2} is there {$count[$name2]} times! <br><br>";

echo "[$name1] is there [$count[$name1]]times! and [$name2] is there [$count[$name2]] times! <br><br>";
?>

This is why i had to learn the function off array_count_values

 

For some terrable reason in_array has not got the abilty to let you

increment the desired search, so if you wanted john twice there no way with in_array

as a solo function...............

 

about time it was added theo.......

 

<?php

$x=array("john","lucky","john","petter","luke","petter","brian","petter");

$name="john";

for($i=2; $i<3; $i++){

if(in_array(".$name[$i].",$x)){

echo" $name[$i] is there";
}
}
?>

Sorry but what the hell are you going on about? in_array is used to see if a value is contained within an array. It wont return how many times that value is in the array. That is why the array_count_value exists.

I agree but would be nice if you was able to have a third pramater to see sometink x times...

 

example

 

<?php
if(in_array("john",$array,2)}{

// what ever

}
?>

 

surley make life easer........

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.