[英]Merging 2 key Arrays into 1 Associative Array
I have 2 arrays, 1 called $person and the other called $classes. 我有2个数组,1个叫$ person,另一个叫$ classes。 I'd like to merge the 2 together into 1 array and assign a name to item in the new merged array.
我想将2合并为1个数组,并为新合并数组中的项目指定名称。 Therefore creating an associative array as opposed to using keys that array_merge seems to be made by default using the array_merge function.
因此,创建一个关联数组,而不是使用array_merge似乎默认使用array_merge函数创建的键。 (For clarity 1 person has multiple classes.)
(为清楚起见,1人有多个班级。)
Currently I have this: 目前我有这个:
($person) ($人)
[1]=>
array(2) {
["Name"]=>
string(1) "Bobby Moore"
["Age"]=>
string(18) "36"
($classes) ($类)
[1]=>
array(2) {
[0]=>
array(11) {
["Class ID"]=>
string(1) "12"
["Class Title"]=>
string(18) "Math 101"
[1]=>
array(11) {
["Class ID"]=>
string(1) "13"
["Class Title"]=>
string(18) "Math 102"
[1]=>
array(11) {
["Class ID"]=>
string(1) "14"
["Class Title"]=>
string(18) "Math 103"
Is it possible to get the following result? 是否有可能得到以下结果?
[person]=>
array(1) {
[0]=>
array(11) {
["Name"]=>
string(1) "Bobby Moore"
["Ages"]=>
string(18) "35"
[Classes]=>
array(2) {
[0]=>
array(11) {
["Class ID"]=>
string(1) "12"
["Class Title"]=>
string(18) "Math 101"
[1]=>
array(11) {
["Class ID"]=>
string(1) "13"
["Class Title"]=>
string(18) "Math 102"
[1]=>
array(11) {
["Class ID"]=>
string(1) "14"
["Class Title"]=>
string(18) "Math 103"
Currently I'm using array_merge($person ,$classes); 目前我正在使用array_merge($ person,$ classes); which unfortunately is an array sorted by keys.
不幸的是,这是一个按键排序的数组。
I hope this makes sense! 我希望这是有道理的! Thank you in advance!
先感谢您!
这不仅仅是一件事
$new_array = array( "person" => $person_array, "classes" => $class_array );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.