php - 将数组添加到现有数组...在另一个数组中?
问题描述
我有以下数组,其中填充了数据库值(此示例中的“PickItems”除外):
$params = array(
'DeliveryCompanyName' => $row['company_name'],
'DeliveryAddress1' => $row['address1'],
'DeliveryAddress2' => $row['address2'],
'DeliveryCity' => $row['city'],
'DeliveryCounty' => $row['province'],
'DeliveryPostCode' => $row['postcode'],
'DeliveryCountry' => $row['country'],
'DeliveryPhone' => $row['phone'],
'DeliveryContact' => $row['first_name'] . " " . $row['last_name'],
'OutboundRef' => $row['order_number'],
'PickItems' => array(
array(
'SKUNumber' => 'SKU',
'Quantity' => '1',
'Comments' => 'Comments'
)
),
'BranchID' => '',
'CustRef' => $row['order_number']
);
在这个数组中,有一个名为“PickItems”的项目,它包含一个数组数组,每个数组都是单独订购的产品。
我试图在另一个数据库查询上使用while循环来填充这些“PickItems”。
这是我开始编写的代码,以便循环遍历包含订单的各个项目的数据库,我需要将它们放入一个数组中并将该数组放入主数组中。我正在努力的部分是将这些值和键放入数组中,我认为 array_push 是要走的路,但另一个问题的另一个答案说你不能这样做。
if ($result2 = $mysqli->query("SELECT * FROM order_items WHERE client_id = {$client} AND main_order_id = {$row['platform_order_id']}")) {
$Items = array();
while($row2 = mysqli_fetch_assoc($result2)) {
//Add the following values to the items array
//'SKUNumber' => $row2['sku'],
//'Quantity' => $row2['quantity'],
//'Comments' => $row2['comments']
}
}
我怀疑主阵列需要更改为这样的东西?:
$params = array(
'DeliveryCompanyName' => $row['company_name'],
'DeliveryAddress1' => $row['address1'],
'DeliveryAddress2' => $row['address2'],
'DeliveryCity' => $row['city'],
'DeliveryCounty' => $row['province'],
'DeliveryPostCode' => $row['postcode'],
'DeliveryCountry' => $row['country'],
'DeliveryPhone' => $row['phone'],
'DeliveryContact' => $row['first_name'] . " " . $row['last_name'],
'OutboundRef' => $row['order_number'],
'PickItems' => array($Items),
'BranchID' => '',
'CustRef' => $row['order_number']
);
解决方案
您可以简单地将其推送到您的 while 循环中:
array_push($params['PickItems'], array(
'SKUNumber' => $row2['sku'],
'Quantity' => $row2['quantity'],
'Comments' => $row2['comments']
));
输出
'PickItems' => array(
array(
'SKUNumber' => 'SKU',
'Quantity' => '1',
'Comments' => 'Comments'
),
array(
'SKUNumber' => 'SKU 2', // Value from $row2['sku']
'Quantity' => '2', // Value from $row2['quantity']
'Comments' => 'Comments2' // Value from $row2['comments']
),
),
欲了解更多信息: http: //php.net/manual/en/function.array-push.php
推荐阅读
- flutter - 如何在 Firebase 存储中上传 PDF 文件并生成下载 URL 并使用 Flutter 保存 Firebase?
- google-apps-script - 如何读取 Google Docs 上的数据并将其写回 Google Sheets(使用应用脚本)
- android-studio - Android studio如何正确删除聊天中的消息
- authentication - .NET 5 Web API 中的 JWT 授权和刷新令牌
- sql - 我需要提取最近 30 天的信息
- angular - Angular 只使用 prod 环境,我该如何解决
- amazon-web-services - 是否可以在 Redshift 中并行运行查询?
- reactjs - 在 React (Gatsby) 中使用 CSS 模块的全局 SCSS
- heap-memory - 如何在 linux 中查看核心转储文件中的堆数据
- go - 如何测试服务器的Oauth2.0资源