Couting occurrences of each value in an array

前端 未结 3 710
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 03:56

I\'m trying to sort and splice an array, however I\'m finding it very confusing.

I have pulled my data from a mysql table:

$total = mysql_query(\"SEL         


        
相关标签:
3条回答
  • 2021-01-17 04:34
    while($result = mysql_fetch_assoc($total))
    {
     $cst[] = $result['customer'];
     $partnumber[] = $result['partnumber'];
     $misc[] = $result['misc'];
    
     if(array_key_exists($result['misc'], $misc_num)) {
       $misc_num[$result['misc']] += 1;
     } else {
       $misc_num[$result['misc']] = 1;
     }
    }
    

    I think that should do it!

    0 讨论(0)
  • 2021-01-17 04:41

    Use array_count_values.

    0 讨论(0)
  • 2021-01-17 04:47

    I am considering your following codes

    $total = mysql_query("SELECT * FROM orders");
    while($result = mysql_fetch_assoc($total))
    {
     $cst[] = $result['customer'];
     $partnumber[] = $result['partnumber'];
     $misc[] = $result['misc'];
    }
    $misc = array(
                  [0] => red,
                  [1] = >red,
                  [2] => blue,
                  [3] => blue,
                  [4] => blue,
                  [5] => green,
                  [6] =>red
                  )

    Now you can implement array_count_values() function here like

    print_r(array_count_values($misc))

    This results the following data

    Array
    (
    [red] => 3
    [blue] => 3
    [green] => 1
    )

    0 讨论(0)
提交回复
热议问题