Hướng dẫn how to add key value in array php - cách thêm giá trị khóa trong mảng php

Tôi đã có một mảng như sau đây có tên $test_package_data. Để tham khảo, tôi đang in hai yếu tố đầu tiên của nó:

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
                                        )

                                )

                        )

                )

        )
)

Bây giờ tôi muốn thêm một cặp giá trị khóa mới có tên [count] => '$some_value' trong mảng này trong mọi yếu tố sau cặp giá trị khóa ________ 6 (giả sử sau [test_pack_purchase_date] => 255. Tôi nên làm điều này như thế nào? Có ai có thể giúp tôi về vấn đề này không? Cảm ơn trước.

Hỏi ngày 8 tháng 7 năm 2013 lúc 10:35Jul 8, 2013 at 10:35

Hướng dẫn how to add key value in array php - cách thêm giá trị khóa trong mảng php

PhploverphploverPHPLover

7.60137 Huy hiệu vàng101 Huy hiệu bạc189 Huy hiệu đồng37 gold badges101 silver badges189 bronze badges

4

Nếu bạn đang tạo mảng mới thì hãy thử điều này:

$arr = ['key' => 'value'];

Và nếu mảng đã được tạo thì hãy thử điều này:

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

Hướng dẫn how to add key value in array php - cách thêm giá trị khóa trong mảng php

Martijn

15,5K4 Huy hiệu vàng36 Huy hiệu bạc67 Huy hiệu Đồng4 gold badges36 silver badges67 bronze badges

Đã trả lời ngày 8 tháng 7 năm 2013 lúc 10:37Jul 8, 2013 at 10:37

dakshbhatt21dakshbhatt21dakshbhatt21

3.5143 huy hiệu vàng31 Huy hiệu bạc39 Huy hiệu đồng3 gold badges31 silver badges39 bronze badges

2

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
   }

}

Đã trả lời ngày 8 tháng 7 năm 2013 lúc 10:48Jul 8, 2013 at 10:48

Hướng dẫn how to add key value in array php - cách thêm giá trị khóa trong mảng php

Goutam Palgoutam PalGoutam Pal

1.7531 Huy hiệu vàng10 Huy hiệu bạc14 Huy hiệu đồng1 gold badge10 silver badges14 bronze badges

Thêm vào:

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

Sau đó, chỉ cần trả lại

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
   }

}
0

Không thể quay lại trực tiếp như cách này

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
   }

}
1

Đã trả lời ngày 7 tháng 12 năm 2020 lúc 17:46Dec 7, 2020 at 17:46

Thử cái này:

foreach($array as $k => $obj) { 
    $obj->{'newKey'} = "value"; 
}

Hướng dẫn how to add key value in array php - cách thêm giá trị khóa trong mảng php

Đã trả lời ngày 2 tháng 4 năm 2018 lúc 13:08Apr 2, 2018 at 13:08

Hướng dẫn how to add key value in array php - cách thêm giá trị khóa trong mảng php

AnithaanithaAnitha

111 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges

1

Array_Keys () được sử dụng trong PHP là gì?

Array_Keys () là một hàm tích hợp trong PHP và được sử dụng để trả về tất cả các khóa và mảng hoặc tập hợp con của các khóa. Tham số: Hàm lấy ba tham số trong đó một tham số là bắt buộc và hai tham số khác là tùy chọn.to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

Cặp giá trị chính trong PHP là gì?

Dấu phẩy sau cặp giá trị khóa cuối cùng là tùy chọn.Phím có thể thuộc loại số nguyên hoặc chuỗi. Giá trị có thể thuộc bất kỳ loại hợp lệ nào. Nếu loại khóa khác là chuỗi hoặc số nguyên, nó sẽ được chuyển vào chuỗi hoặc số nguyên tùy thuộc vào loại khóa.The Key can be of either integer or string type. The Value can be of any valid type. If the type of Key is other that string or integer, it will be cast to string or integer depending on the type of Key.

Array_push trong PHP là gì?

Định nghĩa và cách sử dụng.Hàm mảng_push () chèn một hoặc nhiều phần tử vào cuối một mảng.Mẹo: Bạn có thể thêm một giá trị, hoặc bao nhiêu tùy thích.Lưu ý: Ngay cả khi mảng của bạn có các phím chuỗi, các phần tử được thêm vào của bạn sẽ luôn có các khóa số (xem ví dụ bên dưới).inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).

Làm thế nào tôi có thể nối một mảng vào một mảng khác trong PHP?

Cú pháp của Array array_merge của php ($ mảng1, $ mảng2);Mô tả-Array_merge () là hàm tích hợp ngôn ngữ PHP.$ mảng1 và $ mảng2 là hai mảng chúng tôi đang tìm cách hợp nhất.Nó kết hợp hai mảng duy nhất thành một mảng duy nhất.array_merge($array1, $array2); Description – array_merge() is a PHP language built-in function. $array1 and $array2 are the two arrays we are looking to merge. It combined two single arrays into a single one.