2014-06-07 99 views
-1

我想通過e.g [SUBJECT_CODE] => 05拆分陣列成兩個陣列按值的偶數或奇數5

Array 
(
    [0] => Array 
     (
      [0] => English 
      [subject_name] => English 
      [1] => E-I 
      [subject_abr] => E-I 
      [2] => 
      [ENROL_NO] => 
      [3] => 2013 
      [YEAR_] => 2013 
      [4] => 1 
      [EXAM_CODE] => 1 
      [5] => 42701 
      [ROLL_NO] => 42701 
      [6] => 01 
      [SUBJECT_CODE] => 01   
     ) 

    [1] => Array 
     (
      [0] => English 
      [subject_name] => English 
      [1] => E-II 
      [subject_abr] => E-II 
      [2] => 027-B/FMSGUK-2011 
      [ENROL_NO] => 027-B/FMSGUK-2011 
      [3] => 2013 
      [YEAR_] => 2013 
      [4] => 1 
      [EXAM_CODE] => 1 
      [5] => 42701 
      [ROLL_NO] => 42701 
      [6] => 02 
      [SUBJECT_CODE] => 02   

     ) 

    [2] => Array 
     (
      [0] => Urdu 
      [subject_name] => Urdu 
      [1] => U-I 
      [subject_abr] => U-I 
      [2] => 
      [ENROL_NO] => 
      [3] => 2013 
      [YEAR_] => 2013 
      [4] => 1 
      [EXAM_CODE] => 1 
      [5] => 42701 
      [ROLL_NO] => 42701 
      [6] => 05 
      [SUBJECT_CODE] => 05 

     ) 
) 

回答

1

對於此以下陣列分成奇數和偶數陣列分割,一個簡單的foreach應該足夠了。考慮下面的例子:

// $values is your original array 
$new_values = array(); 
foreach ($values as $key => $value) { 
    if($value['SUBJECT_CODE'] & 1) { 
     $new_values['odd'][] = $value; 
    } else { 
     $new_values['even'][] = $value; 
    } 
} 

echo '<pre>'; 
print_r($new_values); 
echo '</pre>'; 
+0

謝謝你的反應,謝謝你的答覆...哪裏將是新的價值存儲奇數和偶數...我是新來的php請幫助 – user3717373

+0

@ user3717373所有值有奇怪的主題代碼應該在'new_values ['odd']'內,而在本例中甚至應該在'new_values ['even']'內。 – user1978142

+0

非常感謝:x .... – user3717373

1

步驟1:移動通過陣列

$odd = array(); 
$even = array(); 
foreach ($arr as $key => $value) { 
    if ($key % 2 == 0) { 
     $even[] = $value; 
    } 
    else { 
     $odd[] = $value; 
    } 
} 
} 

的奇數值被存儲在$奇數組中,甚至在$甚至。 您可以print_r($奇數)