我正在使用此函数在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
由于
答案 0 :(得分:5)
您可以通过此
获得 13 字词或短语$words = array_map('trim', explode(',', implode(',', array_keys($meta_counts))));
答案 1 :(得分:0)
答案 2 :(得分: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;
感谢