user3512044
user3512044

Reputation: 11

Count how many times a number shows up

Can anyone help me with my problem : I have a variable $info2['server_id'] and when I echo it I get IDs (4,7,10,12) of the servers in the following way : 10 12 7 4 12 7 10 12 4 7 10 4 7 10 12 4 7

and I need to count them and find out how many times is there number 4,7,10,12.

$connect = mysql_connect("$host", "$user", "$pw") or die(mysql_error());  
mysql_select_db("$db") or die(mysql_error());
$data = mysql_query("SELECT * FROM amx_amxadmins WHERE `access`!='bit'") or die(mysql_error());   
while($info = mysql_fetch_array( $data )) {
    $data2 = mysql_query("SELECT * FROM amx_admins_servers WHERE `admin_id`='$info[id]'") or die(mysql_error());
    while($info2 = mysql_fetch_array( $data2 )){
        echo "$info2[server_id]<br />";        
    }
}

Upvotes: 1

Views: 86

Answers (1)

Rafał Walczak
Rafał Walczak

Reputation: 543

Try this:

$ids = array(4, 7, 10, 12);
$server_id = '1012741271012471047101247';
$result = array();
foreach($ids as $id) {
    $result[$id] = substr_count($server_id, $id);
}

You will get associative array, where keys are IDs (4, 7, 10 and 12) and values are numbers of occurrences.

Upvotes: 1

Related Questions