2013-07-08 151 views
21

我有一個名爲$test_package_data的數組。對於參考我打印的首先兩個要素:如何在php中插入一個新的鍵值對數組?

Array 
(
    [0] => Array 
     (
      [test_pack_id] => 9f27643023a83addd5eed41c4aade840 
      [test_pack_name] => Exams Combo 
      [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude. 

Total Tests in this Package : 26 
      [test_pack_type_id] => 3 
      [test_pack_image] => 
      [test_pack_validity_year] => 0 
      [test_pack_validity_month] => 3 
      [test_pack_validity_days] => 0 
      [test_pack_plan] => paid 
      [test_pack_price] => 399.00 
      [test_pack_no_tests] => 0 
      [test_pack_publish] => yes 
      [test_pack_sold] => 1 
      [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8 
      [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675 
      [test_pack_created_date] => 303 
      [test_pack_updated_date] => 1349235701 
      [test_pack_purchase_date] => 255 
      [category_detail] => Array 
       (
        [0] => Array 
         (
          [test_pack_category_id] => 8 
          [category_name] => English Language 
          [test_detail] => Array 
           (
            [0] => Array 
             (
              [test_id] => 91 
              [test_name] => English Language 8 
             ) 

            [1] => Array 
             (
              [test_id] => 90 
              [test_name] => English Language 7 
             ) 

            [2] => Array 
             (
              [test_id] => 88 
              [test_name] => English Language 6 
             ) 

            [3] => Array 
             (
              [test_id] => 89 
              [test_name] => English Language 5 
             ) 

            [4] => Array 
             (
              [test_id] => 87 
              [test_name] => English Language 4 
             ) 

            [5] => Array 
             (
              [test_id] => 86 
              [test_name] => English Language 3 
             ) 

            [6] => Array 
             (
              [test_id] => 85 
              [test_name] => English Language 2 
             ) 

            [7] => Array 
             (
              [test_id] => 97 
              [test_name] => English Language 1 
             ) 

           ) 

         ) 

        [1] => Array 
         (
          [test_pack_category_id] => 4 
          [category_name] => All Subjects 
          [test_detail] => Array 
           (
            [0] => Array 
             (
              [test_id] => 93 
              [test_name] => Mega 2 
             ) 

            [1] => Array 
             (
              [test_id] => 92 
              [test_name] => Mega 1 
             ) 

           ) 

         ) 

        [2] => Array 
         (
          [test_pack_category_id] => 7 
          [category_name] => Quantitative Aptitude 
          [test_detail] => Array 
           (
            [0] => Array 
             (
              [test_id] => 83 
              [test_name] => Quantitative Aptitude 8 
             ) 

            [1] => Array 
             (
              [test_id] => 82 
              [test_name] => Quantitative Aptitude 7 
             ) 

            [2] => Array 
             (
              [test_id] => 81 
              [test_name] => Quantitative Aptitude 6 
             ) 

            [3] => Array 
             (
              [test_id] => 80 
              [test_name] => Quantitative Aptitude 5 
             ) 

            [4] => Array 
             (
              [test_id] => 79 
              [test_name] => Quantitative Aptitude 4 
             ) 

            [5] => Array 
             (
              [test_id] => 78 
              [test_name] => Quantitative Aptitude 3 
             ) 

            [6] => Array 
             (
              [test_id] => 77 
              [test_name] => Quantitative Aptitude 2 
             ) 

            [7] => Array 
             (
              [test_id] => 76 
              [test_name] => Quantitative Aptitude 1 
             ) 

           ) 

         ) 

        [3] => Array 
         (
          [test_pack_category_id] => 6 
          [category_name] => Logical Reasoning 
          [test_detail] => Array 
           (
            [0] => Array 
             (
              [test_id] => 75 
              [test_name] => Reasoning 8 
             ) 

            [1] => Array 
             (
              [test_id] => 74 
              [test_name] => Reasoning 7 
             ) 

            [2] => Array 
             (
              [test_id] => 73 
              [test_name] => Reasoning 6 
             ) 

            [3] => Array 
             (
              [test_id] => 72 
              [test_name] => Reasoning 5 
             ) 

            [4] => Array 
             (
              [test_id] => 71 
              [test_name] => Reasoning 4 
             ) 

            [5] => Array 
             (
              [test_id] => 70 
              [test_name] => Reasoning 3 
             ) 

            [6] => Array 
             (
              [test_id] => 69 
              [test_name] => Reasoning 2 
             ) 

            [7] => Array 
             (
              [test_id] => 68 
              [test_name] => Reasoning 1 
             ) 

           ) 

         ) 

       ) 

     ) 

    [1] => Array 
     (
      [test_pack_id] => e7e95de96987cc7c89c1f0183110fb38 
      [test_pack_name] => Aptitude Reasoning 
      [test_pack_desc] => This package contains 8 tests on Reasoning. 
      [test_pack_type_id] => 3 
      [test_pack_image] => 
      [test_pack_validity_year] => 0 
      [test_pack_validity_month] => 3 
      [test_pack_validity_days] => 0 
      [test_pack_plan] => free 
      [test_pack_price] => 0.00 
      [test_pack_no_tests] => 0 
      [test_pack_publish] => yes 
      [test_pack_sold] => 4 
      [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8 
      [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8 
      [test_pack_created_date] => 303 
      [test_pack_updated_date] => 1347127387 
      [test_pack_purchase_date] => 110 
      [category_detail] => Array 
       (
        [0] => Array 
         (
          [test_pack_category_id] => 6 
          [category_name] => Logical Reasoning 
          [test_detail] => Array 
           (
            [0] => Array 
             (
              [test_id] => 75 
              [test_name] => Reasoning 8 
             ) 

            [1] => Array 
             (
              [test_id] => 74 
              [test_name] => Reasoning 7 
             ) 

            [2] => Array 
             (
              [test_id] => 73 
              [test_name] => Reasoning 6 
             ) 

            [3] => Array 
             (
              [test_id] => 72 
              [test_name] => Reasoning 5 
             ) 

            [4] => Array 
             (
              [test_id] => 71 
              [test_name] => Reasoning 4 
             ) 

            [5] => Array 
             (
              [test_id] => 70 
              [test_name] => Reasoning 3 
             ) 

            [6] => Array 
             (
              [test_id] => 69 
              [test_name] => Reasoning 2 
             ) 

            [7] => Array 
             (
              [test_id] => 68 
              [test_name] => Reasoning 1 
             ) 

           ) 

         ) 

       ) 

     ) 
) 

現在我想的鍵值對[test_pack_purchase_date](後添加一個名爲[count] => '$some_value'新的鍵值對這個陣列中的每個元素的發言權[test_pack_purchase_date] => 255後?我應該怎麼做這個誰能幫助我在這方面預先感謝

+0

你真的不能離開刪除更改順序在陣列中,添加,readding。 – str

+1

你是如何創建數組的? – Dale

+0

array_merge應該幫助 – Robert

回答

67

如果要創建新的數組,那麼試試這個:?

$arr = array("key" => "value");

如果ARR唉已經創建那就試試這個:

$arr["key"] = "value";

+1

當我這樣做時,我只得到最後一個元素... 'for($ i = 1; $ i <= $ 5; $ i ++)$ {EACH_POST_QUERY = mysql_query(「SELECT item_id FROM likes WHERE item_id ='$ i'」); $ EACH_POST_TOTAL_LIKES = mysql_num_rows($ EACH_POST_QUERY); $ EACH_POST_RESULT = array($ i => $ EACH_POST_TOTAL_LIKES); '}' –

+1

@KalpeshSingh:因爲您每次都在創建新數組,所以使用這個'$ EACH_POST_RESULT [] = $ EACH_POST_TOTAL_LIKES更改最後一行; ' 我強烈建議你使用PDO而不是棄用'mysql_connect()',你可以找到更多關於PDO的信息[這裏](https://acomputerengineer.wordpress.com/2013/06/16/pdo-connection -class功能於PHP接力的方法與 - 單例模式/) – dakshbhatt21

9
foreach($test_package_data as $key=>$data) { 

    $category_detail_arr = $test_package_data[$key]['category_detail']; 

    foreach($category_detail_arr as $i=>$value) { 
    $test_package_data[$key]['category_detail'][$i]['count'] = $some_value;////<----Here 
    } 

} 
相關問題