2014-02-06 3 views
0

양식 입력에서 연관 어 레이를 얻으려고합니다. 괜찮습니다. 하지만 키가 배열에 이미 존재한다면 새로운 값을 복제하기를 원하지 않습니다. 여기 내 $ _POST 기능이 모습입니다 :unset 키가 작동하지 않습니다.

<?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) 
) 

.

+0

상향식'해제 (($의 ls_up_vote [$ whatever_the_key_of_arr_is [$ 키])의 하나를 참조하여 루프 또는 해제].' – Wrikken

답변

1
foreach ($ls_up_votes as $k => $v) { 
    foreach ($v as $k2 => $v2) { 
     if (array_key_exists($currentusr->user_login, $v)) { 
      unset($ls_up_votes[$k][$k2]); 
     } 
    } 
} 
+0

덕분에 잘 작동하지만는 키와 값을 제거한다. 빈 배열을 제거하고 배열 번호를 다시 정렬 할 수 있습니까? Array ( [0] => Array() [1] => 배열 ([user1] => 44) [2] => Array() [2] => 배열 ([user2] => 31) )' – Towfiq

관련 문제