如何在php中的数组中插入新的键值对?
Posted
技术标签:
【中文标题】如何在php中的数组中插入新的键值对?【英文标题】:How to insert a new key value pair in array in php? 【发布时间】:2013-07-05 15:47:56 【问题描述】:我有一个名为$test_package_data
的数组,如下所示。作为参考,我打印了它的前两个元素:
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
之后。我应该怎么做?谁能帮我在这方面?提前致谢。
【问题讨论】:
除了删除,添加,读取之外,您无法真正更改数组中的顺序。 你是如何创建数组的? array_merge 应该有帮助 你不能,不是真的。 php assoc 数组本质上是结构。这意味着,为了对键进行排序,必须重建数组。附加密钥然后编写您自己的uksort
函数可能会起作用,或者json_encode
和str_replace
-将您的新键值对放在json_decode($str, true)
之前的正确位置
【参考方案1】:
如果您要创建新数组,请尝试以下操作:
$arr = ['key' => 'value'];
如果数组已经创建,那么试试这个:
$arr['key'] = 'value';
【讨论】:
当我这样做时,我只得到最后一个元素...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);
@KalpeshSingh: 因为你每次都在创建新数组,所以用这个$EACH_POST_RESULT[] = $EACH_POST_TOTAL_LIKES;
更改最后一行我强烈建议你使用 PDO 而不是弃用的mysql_connect()
,你可以找到更多关于PDOhere【参考方案2】:
添加:
$arr["key"] = "value";
然后直接返回$arr
不能这样直接返回return $arr["key"] = "value";
【讨论】:
【参考方案3】:试试这个:
foreach($array as $k => $obj)
$obj->'newKey' = "value";
【讨论】:
这个问题是关于 PHP 关联数组的,这并不能完全回答他们的问题。【参考方案4】: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
【讨论】:
以上是关于如何在php中的数组中插入新的键值对?的主要内容,如果未能解决你的问题,请参考以下文章
使用带有 sql 的键值对搜索 php 多维关联数组,例如 '%LIKE%' 构造