2013-05-07 56 views
-1

我命名$classes_data關聯數組如下:如何在PHP中的關聯數組中插入新的鍵值對?

Array 
(
    [2] => Array 
     (
      [class_id] => 2 
      [class_name] => II 
      [subjects] => Array 
       (
        [0] => 11 Engllish 
       ) 

     ) 

    [3] => Array 
     (
      [class_id] => 3 
      [class_name] => III 
      [subjects] => Array 
       (
        [0] => Hidi 
        [1] => 11 Maths 
        [2] => 11 Science 
        [3] => 11 Engllish 
       ) 

     ) 

    [4] => Array 
     (
      [class_id] => 4 
      [class_name] => IV 
      [subjects] => Array 
       (
        [0] => Physics 
       ) 

     ) 

    [6] => Array 
     (
      [class_id] => 6 
      [class_name] => VI 
      [subjects] => Array 
       (
        [0] => Mathematics 
        [1] => dfadadadsagfasrsarasrarBiology 
       ) 

     ) 

    [7] => Array 
     (
      [class_id] => 7 
      [class_name] => VII 
      [subjects] => Array 
       (
        [0] => Physics 
        [1] => Chemistry11 
        [2] => 11 Science 
       ) 

     ) 

    [8] => Array 
     (
      [class_id] => 8 
      [class_name] => VIII 
      [subjects] => Array 
       (
        [0] => Hidi 
        [1] => 11 Engllish 
       ) 

     ) 

    [9] => Array 
     (
      [class_id] => 9 
      [class_name] => IX 
      [subjects] => Array 
       (
        [0] => Mathematics 
        [1] => Hidi 
        [2] => 11 Science 
       ) 

     ) 

) 

數組的鍵(即2,3,4,6,7,8,9,10-)是在這樣的方式,而不是0,1 ,2,3,4,5,6因爲我使用了一個函數來重新排列這些鍵。

現在我想要做的是插入一個新的密鑰class_checked並將其初始值設置爲0(即class_checked =>"0")。

我嘗試了很多技巧,但無法獲得所需的陣列格式。任何人都可以幫助我獲得理想的數組?提前致謝。

爲了您的信息陣列$classes_data所需的陣列格式如下:

Array 
    (
    [2] => Array 
     (
      [class_id] => 2 
      [class_name] => II 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => 11 Engllish 
       ) 

     ) 

    [3] => Array 
     (
      [class_id] => 3 
      [class_name] => III 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => Hidi 
        [1] => 11 Maths 
        [2] => 11 Science 
        [3] => 11 Engllish 
       ) 

     ) 

    [4] => Array 
     (
      [class_id] => 4 
      [class_name] => IV 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => Physics 
       ) 

     ) 

    [6] => Array 
     (
      [class_id] => 6 
      [class_name] => VI 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => Mathematics 
        [1] => dfadadadsagfasrsarasrarBiology 
       ) 

     ) 

    [7] => Array 
     (
      [class_id] => 7 
      [class_name] => VII 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => Physics 
        [1] => Chemistry11 
        [2] => 11 Science 
       ) 

     ) 

    [8] => Array 
     (
      [class_id] => 8 
      [class_name] => VIII 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => Hidi 
        [1] => 11 Engllish 
       ) 

     ) 

    [9] => Array 
     (
      [class_id] => 9 
      [class_name] => IX 
      [class_checked] => 0 
      [subjects] => Array 
       (
        [0] => Mathematics 
        [1] => Hidi 
        [2] => 11 Science 
       ) 

     ) 

) 

回答

4

嘗試:

foreach($classes_data as $key=>$value) { 
    $classes_data[$key]['class_checked'] = 0; 
} 
+0

謝謝Dharmesh Patel。這是我真正想要的。你的代碼對我來說真的很神奇。再次感謝您的寶貴幫助。 – PHPLover 2013-05-07 06:46:24

3
$array = array_map(function (array $i) { return $i + array('class_checked' => 0); }, $array); 

或者:

foreach ($array as &$i) { 
    $i['class_checked'] = 0; 
} 
unset($i); 
+0

感謝deceze。你的魔法對我來說工作得很好。根據我的要求,我獲得了所需的陣列。再次感謝您的大力幫助。 – PHPLover 2013-05-07 06:43:29

0

給您最初的$classes_data你可以這樣做:

foreach($classes_data as &$class) { 
    $class['class_checked'] = 0; 
} 

現在陣列應該像你想要的那樣。

相關問題