2013-03-29 2 views
0

나는이 다차원 배열이로 다차원 배열 :분할 두 개의 새로운 배열

Array 
(
    [userId] => 35 
    [fieldId] => Array 
     (
      [0] => 1 
      [1] => 2 
      [2] => 3 
      [4] => 4 
     ) 

    [educationTitle] => Array 
     (
      [0] => School1 
      [1] => School2 
      [2] => 3 
      [4] => 
     ) 

    [educationDegree] => Array 
     (
      [0] => Degree1 
      [1] => Degree2 
      [2] => 3 
      [4] => 
     ) 

    [startDate] => Array 
     (
      [0] => 2013-03-01 
      [1] => 2013-03-03 
      [2] => 1970-01-01 
     ) 

    [endDate] => Array 
     (
      [0] => 2013-03-02 
      [1] => 2013-03-04 
      [2] => 1970-01-01 
     ) 

    [educationDescription] => Array 
     (
      [0] => Description1 
      [1] => Description2 
      [2] => 
     ) 

) 

을 내가 matches라는 ID의 배열이 있습니다

[matches] => Array 
     (
      [0] => 1 
      [1] => 2 

     ) 

나는 두 가지로 주요 배열을 분할해야 :

$eduAdd = array() 
$eduUpdate = array() 

$eduAdd에는 일치하지 않는 fieldId의$eduUpdate에는 fieldId의이 포함됩니다.

$eduAdd

이과 같습니다
foreach($filteredSubmittedData as $filteredUpdates){ 
    if(in_array($filteredUpdates['fieldId'], $matches)){ 
     echo "yup"; 
    } 
} 

어떻게하면 할 수

:

Array 
    (
     [userId] => 35 
     [fieldId] => Array 
      (
       [2] => 3 
       [4] => 4 
      ) 

     [educationTitle] => Array 
      (
       [2] => 3 
       [4] => 
      ) 

     [educationDegree] => Array 
      (

       [2] => 3 
       [4] => 
      ) 

     [startDate] => Array 
      (

       [2] => 1970-01-01 
      ) 

     [endDate] => Array 
      (

       [2] => 1970-01-01 
      ) 

     [educationDescription] => Array 
      (

       [2] => 
      ) 

    ) 

내가 in_array 다차원 배열에서 작동하지 않습니다 알아,하지만이 시도 발견?

답변

1

이 주요 배열로 $main을 고려 솔루션 및 $matches, 귀하의 경우 조건을 변경해보십시오 당신의 일치 배열 될 수 :

$eduAdd = array(); 
$eduUpdate = array(); 
$itodel = array(); 
foreach ($main['fieldId'] as $i => $v) 
    if (isset($matches[$i]) and $matches[$i] == $v) 
     $itodel[] = $i; 

foreach ($main as $key => $arr) { 
    if (!is_array($arr)) continue; 
    foreach ($arr as $i => $v) { 
     if (in_array($i, $itodel)) 
      $eduUpdate[$key][$i] = $v; 
     else 
      $eduAdd[$key][$i] = $v; 
    } 
} 

설명

우선 우리는 a를 채울 필요가있다. $main['fieldId'] 안에 매치되는 인덱스의 괘선. 사람들은 $eduUpdate로 이동하고 그 $eduAdd에 삽입되지 않습니다 인덱스입니다

$itodel = array(); 
foreach ($main['fieldId'] as $i => $v) 
    if (isset($matches[$i]) and $matches[$i] == $v) 
     $itodel[] = $i; 

그런 다음 우리가 실제로 다른 두에 $main 배열을 분할합니다 다른 foreach 루프를 실행합니다. 주요 조건은 if (in_array($i, $itodel))입니다. $eduUpdate에 들어가야하는 색인이있는 경우이를 추가해야합니다. 그렇지 않으면 $eduAdd에 삽입하기 만합니다.

+0

헤이 @ 쥬이! 도와 주셔서 감사합니다. 여기에 오타가 있습니다'($ main [ 'fieldId'] $ i = $ v)''=>'내가 변경하고 실행했지만 오류가 발생했습니다 경고 : 잘못된 인수가 foreach()'에 제공되었습니다. 'foreach ($ arr $ i => $ v) {'를 참조하고 있습니다. 그것은 정말로 늦었 어 (2am) 여기에서 I 'll는 이것을 또 한번 본다. 감사! –

+0

@relentless, yup, 당신은 오타가 맞았습니다. 그리고 메인 배열의 첫 번째 요소가 배열이 아니므로'continue '를 추가하여 건너 뜁니다. 둘 다 고정.이제는 작동 할 것입니다. – Shoe

+0

잘 했어! 도와 주셔서 감사합니다! –

1

$filteredUpdates['fieldId'] 자체는 배열 &이므로 in_array는 건초 더미가 필요하므로 예상대로 작동하지 않습니다.

if(array_intersect($filteredUpdates['fieldId'], $matches)){ 
+0

나는 총을 줬지 만 경고 : array_intersect (function [array.intersect] : 인수 # 1은 배열이 아니다.)를 보았습니다. –

+0

- 그러나 주어진 예제 배열 위에있는 것은 정렬. '$ filteredUpdates [ 'fieldId']'가 정확히 들어 왔는지 보여줄 수 있습니까? – Rikesh

+0

수정하십시오. 나는 그것을 두 번 확인해도 위의 내용이 맞습니다. –