找到句子中单词的位置和频率

I wanna find position and frequency of words in sentences. for example : You should eat two bananas before lunch and eat two before your dinner and then consume one banana.

so I will get the result : You -> 1 -> position : 1 Should -> 1 -> position : 2 eat -> 2 -> position : 3, 9 etc.

For get the frequency I can use array_count_values(str_word_count($str, 1))

but how to index to get the location? thank you :)

Well, using your current function, you could just use a foreach loop, then gather them. Use explode and use their indices + 1. Example:

$str = 'You should eat two bananas before lunch and eat two before your dinner and then consume one banana.';

$count = array_count_values(str_word_count($str, 1));
$data = array();
// gather them first
foreach(explode(' ', $str) as $key => $value) {
    $value = str_replace('.', '', $value);
    $key = $key + 1;
    $data[$value]['positions'][] = $key;
    $data[$value]['count'] = $count[$value];
}

?>

<?php foreach($data as $word => $info): ?>
    <p><?php echo "$word -> $info[count] -> Position: " . implode(', ', $info['positions']); ?></p>
<?php endforeach; ?>
$string = 'You should eat two bananas before lunch and eat two before your dinner and then consume one banana';
$array = explode ( ' ' , $string ) ;
$frequency = array_count_values ($array);

    foreach ($frequency as $key => $value)
    {
        echo $key.' -> '.$value.' -> position : '.(array_search($key ,$array)+1).'</br>';
    }