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

you can get 13 words or phrases by this

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

There is a function called explode that lets you split the array.

Here is the link for it.

This should probably do some need.

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

于 2012-12-22T13:14:36.587 に答える
0

Please try code given below

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;

thanks

于 2012-12-22T13:34:38.183 に答える