将新的组合数组推送到 PHP 数组



我有以下代码。这将从 SQL 表 ("db"( 中获取字段,并准备它们以在数据表 ("dt"( 中使用。

$columns = array(
array( 'db' => 'Author', 'dt' => 'authors'),
array( 'db' => 'Editor', 'dt' => 'editor')
);

它打印这个

[{"authors":"John Smith; Paul Phillips","editors":"Robert Fox"},...]

现在我想将第三个数组(people(推送到columns,一个结合了前两个数组但不替换它们的数组,例如

[{"authors":"John Smith; Paul Phillips","editors":"Robert Fox","people": "John Smith: Paul Phillips; Robert Fox"},...]

如果它只是一个你尝试添加到$columns的数组, 使用array_merge((

$columns = array(
array( 'db' => 'Author', 'dt' => 'authors'),
array( 'db' => 'Editor', 'dt' => 'editor')
);
$person = array( 'db' => 'Person', 'dt' => 'person');
$newArray = array_merge($columns, $person);

结果应如下所示:

array(4) {
[0]=>
array(2) {
["db"]=>
string(6) "Author"
["dt"]=>
string(7) "authors"
}
[1]=>
array(2) {
["db"]=>
string(6) "Editor"
["dt"]=>
string(6) "editor"
}
[2]=>
array(2) {
["db"]=>
string(6) "Person"
["dt"]=>
string(6) "person"
}
}

最新更新