[Solved] How to get popular keywords in an array


array_count_values will output the count as like

Array
(
    [keyword1] => 10
    [keyword2] => 3
    [keyword3] => 6
    [keyword4] => 5
    [keyword5] => 1
)

But For your desired output you need to use foreach

Demo

$arra = Array
(
    0 => "keyword1",
    1 => "keyword1",
    2 => "keyword1",
    3 => "keyword1",
    4 => "keyword1",
    5 => "keyword1",
    6 => "keyword1",
    7 => "keyword1",
    8 => "keyword1",
    9 => "keyword1",
    10 => "keyword2",
    11 => "keyword2",
    12 => "keyword2",
    13 => "keyword3",
    14 => "keyword3",
    15 => "keyword3",
    16 => "keyword3",
    17 => "keyword3",
    18 => "keyword3",
    19 => "keyword4",
    20 => "keyword4",
    21 => "keyword4",
    22 => "keyword4",
    23 => "keyword4",
    24 => "keyword5"
);

$newarray = array_count_values($arra);
$output = array();
foreach($newarray as $key=>$val) {
    $output[] = array("0"=>$key, "count"=>$val);
}

echo '<pre>';print_r($output);echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [0] => keyword1
            [count] => 10
        )

    [1] => Array
        (
            [0] => keyword2
            [count] => 3
        )

    [2] => Array
        (
            [0] => keyword3
            [count] => 6
        )

    [3] => Array
        (
            [0] => keyword4
            [count] => 5
        )

    [4] => Array
        (
            [0] => keyword5
            [count] => 1
        )

)

solved How to get popular keywords in an array