2015-11-28 38 views
0

我正在爲我的用戶配置文件開發註釋部分,我正在給用戶提供一個選項以使註釋可見或不可見。更新用戶元數組

我創建了一個數組:

$comment = array($comment_name, $comment_text, $time, $visible); 

其中$可見的是,在默認值爲false。 然後我add_user_meta

add_user_meta($user->ID, 'recommend_comment', $comment); 

這是爲我工作完美,我有顯示的註釋的數組。 現在我想用$ visible = true更新數組,如果用戶單擊按鈕但不確定如何訪問具有update_user_meta的特定數組行。 我嘗試過:

update_user_meta($user->ID, 'recommend_comment', $prikazi, [2]); 

但這不起作用。任何想法如何做到這一點?

回答

1

您可以使用update_user_meta()用於添加和/或更新,請參考:update_user_meta

要更新可見你可以這樣做:

$comment = get_user_meta($user->ID, 'recommend_comment', TRUE); 
if(!empty($comment)) { 
    $comment[3] = FALSE; 
    update_user_meta($user->ID, 'recommend_comment', $comment); 
} 

提高了一點,你可以改用例如:

$comment = array('name' => $comment_name, 'text' => $comment_text, 'time' => $time, 'visible' => $visible); 
// And then you can access with: 
$comment['visible'] = TRUE; 

更新:示例與評論列表:

$comments = array(
    array('name' => 'AAA', 'text' => 'Just a comment', 'time' => '12:50', 'visible' => FALSE), 
    array('name' => 'BBB', 'text' => 'Another one', 'time' => '14:10', 'visible' => TRUE), 
); 
// Create/updates the comments 
update_user_meta($user->ID, 'recommend_comment', $comments); 
// ... 
// Load the comments 
$comments = get_user_meta($user->ID, 'recommend_comment', TRUE); 
if(!empty($comment)) { 
    // then you can manipulate them with: 
    $comments[1]['visible'] = FALSE; 
    // and update the meta as before 
    update_user_meta($user->ID, 'recommend_comment', $comments); 
} 
+0

我get_user_meta將返回此: http://pokit.org/get/img/40edd6c58f2e2be7931ecaeabc58e82b.jpg 我需要以某種方式更新陣列只有一行。 – Gifron

+0

你可以簡單地做:'$ comments [1] ['visible'] = TRUE;',然後你更新元,如前所示,數組的comments數組將被序列化在用戶元選項 – Mat

+0

數據去完成混亂。 '$ comment [0] ['visible'] = TRUE;' 'update_user_meta($ user-> ID,'recommend_comment',$ comment); ' http://pokit.org/get/img/67fbb2386af46239394fc18556ed64c8.jpg – Gifron