2017-12-14 5 views
1

私は以下のコードを使用していますが、出力が必要です。例外出力例特定のJSON形式のPHParray

<?php 
$text = "A very nice únÌcÕdë text. Something nice to think Something about if you're into Unicode."; 
$words = str_word_count($text, 1); // use this function if you only want ASCII 
$frequency = array_count_values($words); 
arsort($frequency); 
print_r($frequency); 
?> 


**Current output** 
    Array 
(
    [nice] => 2 
    [Something] => 2 
    [A] => 1 
    [very] => 1 
    [n] => 1 
    [c] => 1 
    [d] => 1 
    [text] => 1 
    [to] => 1 
    [think] => 1 
    [about] => 1 
    [if] => 1 
    [you're] => 1 
    [into] => 1 
    [Unicode] => 1 
) 

**Excepted output Below :** 

{ 
    { 
     ‘word’ : ‘nice’ 
     ‘count’ : 2 
     ‘rank’ : 1 
    }, 
    {    
     ‘word’ : ‘Something’ 
     ‘count’ : 2 
     ‘rank’ : 2 
    }, 
    {    
     ‘word’ : ‘A’ 
     ‘count’ : 1 
     ‘rank’ : 3 
    } 
} 

and so on ... etc 

以下は、私はそれを達成することができますどのように私を助けたり、私がここで間違っ

やって聞かせてくださいすることができ、私は以下のコードを使用していますが、私のような除外出力としての私のJの息子形式のいくつかで出力を必要とします
+0

? –

+0

が1番、2番が最高です... 2番など... jsonはサンプル用です。私はちょうど –

+0

というフォーマットですので、 'arsort($ frequency);'の後ろに配列してください。 '$ frequency'を出力し、データを表示します。 (あなたの質問に追加してください) –

答えて

1

私はこれがあなたの望むものだと思います。私は

$text = "A very nice únÌcÕdë text. Something nice to think Something about if you're into Unicode."; 
$words = str_word_count($text, 1); // use this function if you only want ASCII 
$frequency = array_count_values($words); 
arsort($frequency); 

$new_arr = array(); 

$rank_counter = 1; 
foreach ($frequency as $key => $value) { 
    $new_arr[] = array(
     'word' => $key, 
     'count' => $value, 
     'rank' => $rank_counter++, 
    ) 
} 

$myJSON = json_encode($new_arr); 
echo "<pre>"; 
print_r($myJSON); 

$頻度で新しい配列を作成し、出力

の順位が決定される方法
[ 
    { 
     word: "nice", 
     count: 2, 
     rank: 1 
    }, 
    { 
     word: "Something", 
     count: 2, 
     rank: 2 
    }, 
    { 
     word: "A", 
     count: 1, 
     rank: 3 
    }, 
    { 
     word: "very", 
     count: 1, 
     rank: 4 
    }, 
    { 
     word: "n", 
     count: 1, 
     rank: 5 
    }, 
    { 
     word: "c", 
     count: 1, 
     rank: 6 
    }, 
    { 
     word: "d", 
     count: 1, 
     rank: 7 
    }, 
    { 
     word: "text", 
     count: 1, 
     rank: 8 
    }, 
    { 
     word: "to", 
     count: 1, 
     rank: 9 
    }, 
    { 
     word: "think", 
     count: 1, 
     rank: 10 
    }, 
    { 
     word: "about", 
     count: 1, 
     rank: 11 
    }, 
    { 
     word: "if", 
     count: 1, 
     rank: 12 
    }, 
    { 
     word: "you're", 
     count: 1, 
     rank: 13 
    }, 
    { 
     word: "into", 
     count: 1, 
     rank: 14 
    }, 
    { 
     word: "Unicode", 
     count: 1, 
     rank: 15 
    } 
] 
+0

をチェックしてください1:2:3:etc –

+0

を削除することは可能ですか? – Thamaraiselvam

+1

完璧に感謝:)とても速い –