2014-07-05 2 views
0

사용자가 문자열에서 데이터를 입력하는 양식을 작성하려고하면 폼이 입력을 기반으로 json src를 통해 데이터의 하위 집합을 가져 와서 다차원 배열을 만들고 데이터베이스에 저장하십시오. 입력이 이전에 데이터베이스에 추가 된 경우 배열에 다시 추가하려고하지 않습니다. 여기키가 존재하는 경우 다차원 배열의 배열 바꾸기

내 코드는 모습입니다 :

//SET UP the Array 
$thisquery_themes[] = array(
    strtolower($themename) => array(
     "author"=>$data['Author'], 
     "desc"=>$data['Description'], 
     "screenshot"=> $screenshot, 
     "link"=> $data['URI'], 
     "count"=> 1 
    ) 
); 

//Get Previously saved data 
$existing_themes = get_option('top_themes'); 

if(!empty($existing_themes)){ 
    foreach ($existing_themes as $group){ 
     foreach(array_keys($group) as $key) { 
      if($group[strtolower($themename)] == strtolower($themename)){ 
       unset($group[$key][strtolower($themename)]); 
      } 
     } 
    } 

    $total_themes= array_merge($existing_themes , $thisquery_themes); 
    update_option('top_themes', $total_themes); 
} else { 
    update_option('top_themes', $thisquery_themes); 
} 

는하지. 키가 배열에 존재하는 경우, 데이터는 여전히 배열에 추가되는 :

Array ( 
[0] => Array ( 
     [towfiq-i._v5] => Array ( 
      [author] => Towfiq I. 
      [desc] => Towfiq I. official website. 
      [count] => 1 
     ) 
) 
[1] => Array ( 
     [towfiq-i._v5] => Array ( 
      [author] => Towfiq I. 
      [desc] => Towfiq I. official website. 
      [count] => 1 
     ) 
) 
[2] => Array ( 
     [wp-bangla] => Array ( 
      [author] => Ifty Rahman 
      [desc] => A website template for wpbangla 
      [count] => 1 
     ) 
) 
[3] => Array ( 
     [towfiq-i._v5] => Array ( 
      [author] => Towfiq I. 
      [desc] => Towfiq I. official website. 
      [count] => 1 
     ) 
) 
[4] => Array ( 
     [wp-bangla] => Array ( 
      [author] => Ifty Rahman 
      [desc] => A website template for wpbangla 
      [count] => 1 
     ) 
) 

그러나 나는 (이렇게 될 필드 값이 합산 방법 "계산"주의 할 그 가능하면 알려주세요.) :

Array ( 
[0] => Array ( 
     [towfiq-i._v5] => Array ( 
      [author] => Towfiq I. 
      [desc] => Towfiq I. official website. 
      [count] => 3 
     ) 
) 

[1] => Array ( 
     [wp-bangla] => Array ( 
      [author] => Ifty Rahman 
      [desc] => A website template for wpbangla 
      [count] => 2 
     ) 
) 

어떤 도움을 주시면 감사하겠습니다. 감사합니다

답변

1

왜 하나의 값만 보유 할 예정이라면 배열 내에서 배열을 사용하고 있습니까? 당신이 배열 요소가 isset($existing_themes[strtolower($themename)])를 사용하여 존재하는지 확인 수 있기 때문에

Array ( 
     [towfiq-i._v5] => Array ( 
      [author] => Towfiq I. 
      [desc] => Towfiq I. official website. 
      [count] => 3 
     ) 
     [wp-bangla] => Array ( 
      [author] => Ifty Rahman 
      [desc] => A website template for wpbangla 
      [count] => 2 
     ) 
) 

그런 다음 당신의 세계를 훨씬 쉽게 될 것입니다 : 그건 당신에 던져지는 무슨이 아니라면, 당신의 구조는 쉽게처럼 보일 수있다 (예를 들어.)

당신이 $existing_themes을받을 방법을 변경할 수없는 경우는 다음과 같이 데이터를 포맷 할 수 있습니다 :

$existing_themes_mod = array(); 
foreach ($existing_themes as $t) { 
    $existing_themes_mod[key($t)] = reset($t); 
} 

는이 작업을위한 전략의 충분한을 제공해야 한는 "업데이트"를 실행할 수 기존 항목 대신 부속 장치. 그것을 작성하는 데 도움이 필요하면, 먼저 귀하의 시도를 보여주십시오.

수정 - 코드를 붙여 주셔서 감사합니다. 여기에서 당신의 목표를 달성하기 위해 더 나은 방법은 ... 그런데 모든 공백을 조심해야한다

if (!empty($existing_themes)) { 
    if (isset($existing_themes[strtolower($themename)])) { 
     if (isset($existing_themes[strtolower($themename)]['count'])) { 
      $existing_themes[strtolower($themename)]['count'] = $existing_themes[strtolower($themename)]['count'] + 1; 
     } else { 
      $existing_themes[strtolower($themename)]['count'] = 1; 
     }            
     $thisquery_themes = array(); 
    }        
    $total_themes= array_merge($existing_themes , $thisquery_themes); 
    update_option('top_themes', $total_themes);      
} else { 
    update_option('top_themes', $thisquery_themes); 
} 

작동하지 않았다 당신의 시도가 당신하여 증가하여 $value을 변경하려고하면 때문입니다 이유 1, 원래 배열을 수정하지 않습니다. 이와 같이 foreach을 사용하면 참조로 반복해야합니다. 그렇지 않으면 값을 수정할 수 없습니다. 너 혼자 너무 많은 일을하고있어!

+0

우수. 왜 배열 구조를 복잡하게 만들었는지 모르겠다. 지적 해 주셔서 고마워. 나는 너의 sugestion을 가지고 가고 중복 입장 문제점을 해결했다. 내가 겪고있는 유일한 문제는 카운트 필드를 업데이트하는 것입니다. 내 코드의 모습은 다음과 같습니다. http://pastebin.com/qBSH91Js. 감사합니다 – Towfiq

+0

@Towfiq 당신을 위해 편집했습니다 – sjagr

+0

우수! 매력처럼 일 했어! – Towfiq

관련 문제