Я пытаюсь получить ассоциативный арай из ввода формы, который работает нормально. Но я не хочу, чтобы новое значение дублировалось, если ключ уже существует в массиве. Вот как мой $ _POST функция выглядит:unset array key not working
<?php // top of page
if (isset($_POST['drw_inventory']) && wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta'))
{
//if nonce check succeeds.
global $post;
$postid = $post->ID;
$data = $_POST['wpd_function_rating'];
$currentusr = wp_get_current_user();
//Get the Existing user ratings of this post
$ls_up_votes = (get_post_meta($postid, 'wpd_rating', TRUE));
//if the current user already rated, unset the rating
foreach($ls_up_votes as $arr) {
foreach($arr as $key => $value) {
if (array_key_exists($currentusr->user_login, $arr)) {
unset($arr[$key]);
}
}
}
//Add post meta 'wpd_rating' with this structure:
$ls_up_votes[] = array($currentusr->user_login => $data);
update_post_meta($postid,'wpd_rating',$ls_up_votes);
}
?>
Форма
<form method="post" action="">
<?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>
<label>This is label</label>
<input type='text' name='wpd_function_rating' value='' />
<input type='submit' value='save' />
</form>
Это как мой массив выглядит (вы можете увидеть повторяющиеся пользовательские входные данные, я хочу, чтобы значение массива будет обновляться на существующий ключ пользователя):
Array (
[0] => Array ([user1] => 33)
[1] => Array ([user1] => 44)
[2] => Array ([user2] => 34)
[2] => Array ([user2] => 31)
)
Любая помощь будет принята с благодарностью.
Либо цикл по ссылке, или отключенное снизу вверх 'снята с охраны (($ ls_up_vote [$ whatever_the_key_of_arr_is] [$ ключа]);.' – Wrikken