2010-12-29 2 views
1

배열을 보내는 $ _POST가 있습니다. 그리고 내가 가지고있는 prevoious 배열은 teh $ _POST의 값 중 하나를 포함 할 수 있거나 포함 할 수없는 키를 포함합니다.다차원 배열을 간단한 배열과 비교하여 차이를 추출하십시오.

예를 들어

:

$_post: Array ([0] => 13 [1] => 10 [2] => 52) 
Previous: Array ([0] => Array ([collection_id] => 13 [artwork_id] => 21) 
        [1] => Array ([collection_id] => 11 [artwork_id] => 21)) 

그래서 나는 $ _POST 아이튠즈 뮤직 스토어는 이미 previuos 배열에 존재하는지 확인 ([collection_id를] 키) 및 추가 한 (이 경우 [1] => 10 [2] => 52에) 새로운 사람을 추출해야 을 데이터베이스에서 제거하고 변경된 데이터베이스를 새 값으로 대체 (대체)해야합니다.

이 내 현재 코드 만 잘 작동하지 ...

  $new_nodes = array(); 

      $i = 0; 
      foreach($old_nodes as $node){ 
       foreach ($collections as $collection) { 
        $new = array('collection_id' => $collection, 'artwork_id' => $artwork['id']); 
        if(array_diff($node, $new)){ 
         if($collection > 0){ 
          array_push($new_nodes, $new); 
         } 
        } 
        else{ 
         unset($old_nodes[$i]); 
        } 
       } 
       $i++; 
      } 


      foreach($new_nodes as $node){ 
       for ($i = 0; $i <= count($new_nodes); $i++) { 
        if(isset($new_nodes[$i])){ 
         if(!array_diff($node, $new_nodes[$i])){ 
          unset($new_nodes[$i]); 
         } 
        } 
       } 
      } 

참고 : old_nodes는 "이전"과 $ 컬렉션은 "$ _POST는"

+0

'[1] => 10 [2] => 52'를 추가 한 후 배열은 어떻게 생겼을까요? 새 하위 배열이되거나 하위 배열 중 하나에 추가됩니까? –

답변

0

는 다음과 같이 시도입니다 :

$old_nodes = array(); 
$new_nodes = array(); 
$del_nodes = array(); 

foreach ($collections as $collection) { 
    array_push($old_nodes, $collection['collection_id']); 
} 

$new_nodes = array_diff($collections, $old_nodes); 

$del_nodes = array_diff($old_nodes, $collections); 
관련 문제