我有这个数组:
Array
(
[key] => 123456
[siteNumber] => 123456
[useBalanceOnly] => 80
[coupon] => free50
[deliveryAddress.countryId] => IN
[deliveryAddress.state] => GUJ
[deliveryAddress.city] => Ahmedabad
[deliveryAddress.company] => abc
[deliveryAddress.address] => abc
[deliveryAddress.address2] => abc
[deliveryAddress.postcode] => 123
[deliveryAddress.receiver] => 1
[deliveryAddress.telephone] => 123456789
[deliveryAddress.shippingWayId] => 2
[0] => Array
(
[0] => Array
(
[items.1.itemNo] => 51
[items.1.qty] => 8
)
[1] => Array
(
[items.2.itemNo] => 52
[items.2.qty] => 2
)
)
)
我想把它变成这样:
Array
(
[key] => 123456
[siteNumber] => 123456
[useBalanceOnly] => 80
[coupon] => free50
[deliveryAddress.countryId] => IN
[deliveryAddress.state] => GUJ
[deliveryAddress.city] => Ahmedabad
[deliveryAddress.company] => abc
[deliveryAddress.address] => abc
[deliveryAddress.address2] => abc
[deliveryAddress.postcode] => 123
[deliveryAddress.receiver] => 1
[deliveryAddress.telephone] => 123456789
[deliveryAddress.shippingWayId] => 2
[items.1.itemNo] => 51
[items.1.qty] => 8
[items.2.itemNo] => 52
[items.2.qty] => 2
)
这是我到目前为止的代码:
$p = $db->query("select * from table");
$no = 0;
while($row = $p->fetch_array()){
$no = $no +1;
$user_result[] = array(
"items.".$no.".itemNo" => $row['itemno'],
"items.".$no.".qty" => $row['itemqty']
);
}
$parameters = array(
'key' => 123456,
'siteNumber' => '123456',
'useBalanceOnly' => '80',
'coupon' => 'free50',
'deliveryAddress.countryId' => 'IN',
'deliveryAddress.state' => 'GUJ',
'deliveryAddress.city' => 'Ahmedabad',
'deliveryAddress.company' =>'abc',
'deliveryAddress.address' => 'abc',
'deliveryAddress.address2' => 'abc',
'deliveryAddress.postcode' => '123',
'deliveryAddress.receiver' => '1',
'deliveryAddress.telephone' => '123456789',
'deliveryAddress.shippingWayId' => '2',
$user_result
);
print_r($parameters);
答案 0 :(得分:1)
使用array_merge(),它将结果合并到数组中。 (或直接使用array operators)。您还希望以不同的方式设置$user_result
的格式,以使其形成一个数组,而不是子数组:
$p = $db->query("select * from table");
$no = 0;
$user_result = array();
while($row = $p->fetch_array()){
$no = $no +1;
$user_result["items.".$no.".itemNo"] = $row['itemno'];
$user_result["items.".$no.".qty"] = $row['itemqty'];
}
$parameters = array(
'key' => 123456,
'siteNumber' => '123456',
'useBalanceOnly' => '80',
'coupon' => 'free50',
'deliveryAddress.countryId' => 'IN',
'deliveryAddress.state' => 'GUJ',
'deliveryAddress.city' => 'Ahmedabad',
'deliveryAddress.company' =>'abc',
'deliveryAddress.address' => 'abc',
'deliveryAddress.address2' => 'abc',
'deliveryAddress.postcode' => '123',
'deliveryAddress.receiver' => '1',
'deliveryAddress.telephone' => '123456789',
'deliveryAddress.shippingWayId' => '2',
) + $user_result;
就我个人而言,我只是将其堆叠到参数数组的末尾,而不是完全形成2个数组:
$parameters = [
'key' => 123456,
'siteNumber' => '123456',
'useBalanceOnly' => '80',
'coupon' => 'free50',
'deliveryAddress.countryId' => 'IN',
'deliveryAddress.state' => 'GUJ',
'deliveryAddress.city' => 'Ahmedabad',
'deliveryAddress.company' => 'abc',
'deliveryAddress.address' => 'abc',
'deliveryAddress.address2' => 'abc',
'deliveryAddress.postcode' => '123',
'deliveryAddress.receiver' => '1',
'deliveryAddress.telephone' => '123456789',
'deliveryAddress.shippingWayId' => '2',
];
$p = $db->query("select * from table");
$no = 0;
while($row = $p->fetch_array()){
$no++; // Or $no += 1; Or $no = $no + 1;
$parameters["items.{$no}.itemNo"] = $row['itemno'];
$parameters["items.{$no}.qty"] = $row['itemqty'];
}
答案 1 :(得分:-1)
实际上,您将$ user_result数组作为大数组中的一项进行制作。您需要将它们合并。
您是否尝试过数组合并?
array_merge($parameters,$user_result);
答案 2 :(得分:-1)
如果您希望最终结果是平面(一维)数组,请在获取结果之前定义$parameters
数组,而不是将结果添加到单独的数组并将该数组附加到$parameters
,只需将新数据添加到$parmameters
:
$parameters = array(
'key' => 123456,
// rest of $parameters array
'deliveryAddress.shippingWayId' => '2',
);
$no = 0;
while($row = $p->fetch_array()){
$no = $no +1;
//do not add new array to the $user_data, simply create new keys
$parameters ["items.{$no}.itemNo"] = $row['itemno'];
$parameters ["items.{$no}.qty"] = $row['itemqty'];
}
或者,您可以构建另一个数组,例如$user_data
,但避免使其成为多维数组(即,不要为每行向$user_data
添加新数组,因为无论如何您都在创建唯一索引)然后使用array_merge
或+
运算符将它们合并。
但是,如果您确实需要$user_data
作为多维数组(例如,现在在代码中的方式,例如,如果该数组的唯一用途不附加到$parameters
并且您将按原样使用它)可以尝试flatten it,然后将其与$parameters
合并。
答案 3 :(得分:-2)
if you want itemno and itemqtty in aequence try this one:
$parameters = array(
'key' => 123456,
'siteNumber' => '123456',
'useBalanceOnly' => '80',
'coupon' => 'free50',
'deliveryAddress.countryId' => 'IN',
'deliveryAddress.state' => 'GUJ',
'deliveryAddress.city' => 'Ahmedabad',
'deliveryAddress.company' =>'abc',
'deliveryAddress.address' => 'abc',
'deliveryAddress.address2' => 'abc',
'deliveryAddress.postcode' => '123',
'deliveryAddress.receiver' => '1',
'deliveryAddress.telephone' => '123456789',
'deliveryAddress.shippingWayId' => '2',
);
$p = $db->query("select * from table");
$no=0;
while($row = $p->fetch_array())
{
$no++;
$parameters["items.{$no}.itemNo"] => $row['itemno'],
}
$no1 = 0;
while($row1 = $p->fetch_array())
{
$no1++;
$parameters["items.{$no1}.itemNo"] => $row1['itemqty']
}
print_r($parameters);