添加/更新并显示meta_values数组

时间:2017-11-14 13:30:29

标签: mysql arrays wordpress

我正在尝试将meta_values数组添加到单个meta_key中,并将其显示在列表中,如下所示:

  • 评估一,通过
  • 评估二,通过

我的更新/添加user_meta的代码:

    $user_id = $current_user->ID;
    $key = 'assessment_result';
    $assessment_name = get_the_title();
    $new_value = array($assessment_name, 'Passed');
    $meta = get_user_meta($user_id, $key, false);
    if ( ! array($meta) ) {
        $meta = array();
    }
    $meta[] = $new_value;
    update_user_meta($user_id, $key, $meta, $unique);

完成两次评估后,我的数组看起来像这样:

Array ( [0] => Array ( [0] => Array ( [0] => Array ( [0] => Assessment Two [1] => Passed ) ) [1] => Array ( [0] => Assessment One [1] => Passed ) ) ) 1 

阵列似乎太多了!

然后我的查询如下:

$user_assessment_results = get_user_meta($user_id, 'assessment_result', true);
echo '<ul>';
foreach ($user_assessment_results as $user_assessment_result) {
    echo '<li>';
    $output = array();
    foreach ($user_assessment_result as $result_item) {
        $output[] =  $result_item;
    }
    if( count( $output ) ) echo join( ", ", $output );
    echo '</li>';
}
echo '</ul>';

结果是:

  • 阵列
  • 评估一,通过

你能告诉我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

我发现问题$meta = get_user_meta($user_id, $key, false);应该是$meta = get_user_meta($user_id, $key, true);