php - 无法将值添加到关联数组 php
问题描述
我在关联数组的第一个位置添加值时遇到问题,我尝试在互联网上搜索,使用了几种方法来添加值,但仍然没有在第一个位置添加值。
我尝试的是
array_unshift($output_countries['CountryName'], "India");
但它不起作用
我的 PHP 代码:
while($rows_fetch_country_list = mysql_fetch_array($query_country_list)) {
extract($rows_fetch_country_list);
$output_countries[] = array(
"CountryName" => $country_name,
"CountryCode" => $country_code,
"Id" => $pk_country_id
);
}
有什么建议么?谢谢!
解决方案
以下是如何为现有数组添加值。使用 unshift 是正确的,但您可能希望静态值看起来像您的数据库记录,并且 unshift 的第一个参数应该只是目标数组。
<?php
//Your static record
$staticCountry = ['CountryName' => 'India', 'CountryCode' => 'IN', 'Id' => 0];
//Results from DB
$countries = [
['CountryName' => 'Canada', 'CountryCode' => 'CA', 'Id' => 1],
['CountryName' => 'France', 'CountryCode' => 'FR', 'Id' => 2],
['CountryName' => 'Germany', 'CountryCode' => 'DE', 'Id' => 3]
];
array_unshift($countries, $staticCountry);
推荐阅读
- r - 使用 plm 前需要声明面板数据吗?
- sql - Postgres重复名称不同的电话号码
- python - 更改 OneLineListItem MDList 的 font_name
- angular-dart - 在角度飞镖中,如何使向上/向下箭头键在列表中导航?
- email - 画布不发送邮件。关于outgoing_mail.yml 的疑问
- c# - Microsoft Graph API - 批准审核请求
- mongodb - 需要帮助查询嵌套字段的不同组合
- glsl - 错误:“子组操作”:需要 SPIR-V 1.3
- python - 捕获贝宝订单时如何呈现模板?
- php - Symfony 4.4 Easyadmin:设置权限以仅访问我拥有的实体