php - 将新的组合数组推送到 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"
}
}
推荐阅读
- matlab - 我必须将音频信号分割成几个片段,我应该可以访问每个片段
- scala - 使用 scala 读取 csv 文件并生成分析
- java - 使用 Hibernate 实体的域实体?
- angular - Angular 模块导入它不使用的模块的潜在缺点?
- python - struct.pack 显示移位数据
- python - 缺少 1 个必需的位置参数:'....'
- .net-core - 当我们有 IDistributedCache 时,为什么要使用 IMemoryCache?
- javascript - 如何使用模板文字作为对象文字内的键?
- r - MSTS和预测功能
- flutter - 预期类型为“列表”的值
>',但得到了“MappedListIterable”类型之一 >'