1

我正在使用此函数在 WordPress 中获取一组自定义元字段

$my_var = get_meta_values( 'keywords' );
if( !empty( $my_var ) ) {
    $meta_counts = array();
    foreach( $my_var as $meta_value )
        $meta_counts[$meta_value] = ( isset( $meta_counts[$meta_value] ) ) ? $meta_counts[$meta_value] + 1 : 1;
}
print_r ($meta_counts);

它生成的数组看起来像

Array ( 
  [one, two, three, four and five, six and seven] => 1 
  [clean, ajax one, two three, four five] => 1 
  [] => 1 
  [this is a test, working] => 1 
  [asdfasdf] => 1 
  [last test] => 1 
)

如何获得以逗号分隔的每个单词或短语的总数。不是每个单词。在上面的数组中,计数为 13

谢谢

4

3 回答 3

5

你可以通过这个得到13个单词或短语

$words = array_map('trim', explode(',', implode(',', array_keys($meta_counts))));
于 2012-12-22T13:29:21.930 回答
0

请尝试下面给出的代码

if( !empty( $my_var ) ) {
    $meta_counts = array();
    $total_count = 0;  
    foreach( $my_var as $meta_index=>$meta_value ){
         if(isset($meta_index) && !empty($meta_index)){
            $meta_index_arr = explode(',', $meta_index);
            $meta_counts[$meta_index] = count($meta_index_arr);   
            $total_count += $meta_counts[$meta_index];
         }else{
                          $meta_counts[$meta_index] = 0;
        }
     }

}
print_r ($meta_counts);
echo $total_count;

谢谢

于 2012-12-22T13:34:38.183 回答
0

有一个名为 explode 的函数可以让您拆分数组。

这是它的链接。

这可能应该做一些需要。

http://php.net/manual/en/function.explode.php

于 2012-12-22T13:14:36.587 回答