我有2个数组,如下所示
$array1 = Array
(
[0] => stdClass Object
(
[CUSTOMERTRXLINEID] => 2683959
[LINETYPE] => LINE
[LINEAMOUNT] => 153000
[TAXAMOUNT] => 0
[INVOICEAMOUNT] => 153020
[CREDITEDAMOUNT] =>
[ORIGINALLINEAMOUNT] => 153000
[DESCRIPTION] => Silver Office Package Property#W 101,from 11-02-2018 to 10-02-2019 Area52.5
)
[1] => stdClass Object
(
[CUSTOMERTRXLINEID] => 2683960
[LINETYPE] => LINE
[LINEAMOUNT] => 10
[TAXAMOUNT] => 0
[INVOICEAMOUNT] => 153020
[CREDITEDAMOUNT] =>
[ORIGINALLINEAMOUNT] => 10
[DESCRIPTION] => Knowledge Fees (As per Government of Dubai - Law(4) 2005
)
[2] => stdClass Object
(
[CUSTOMERTRXLINEID] => 2683961
[LINETYPE] => LINE
[LINEAMOUNT] => 10
[TAXAMOUNT] => 0
[INVOICEAMOUNT] => 153020
[CREDITEDAMOUNT] =>
[ORIGINALLINEAMOUNT] => 10
[DESCRIPTION] => Innovation Fees (As per Government of Dubai - Law(20) 2015)
)
)
第二个数组为
$array2 = Array
(
[0] => 68850.00
[1] => 10
[2] => 10
)
我想在第一个数组的每个对象中再创建一个条目[APPROVEDAMT] =>
,并将第二个数组的值存储到其中。我希望结果数组如下所示
$result_array = Array
(
[0] => stdClass Object
(
[CUSTOMERTRXLINEID] => 2683959
[LINETYPE] => LINE
[LINEAMOUNT] => 153000
[TAXAMOUNT] => 0
[INVOICEAMOUNT] => 153020
[CREDITEDAMOUNT] =>
[ORIGINALLINEAMOUNT] => 153000
[DESCRIPTION] => Silver Office Package Property#W 101,from 11-02-2018 to 10-02-2019 Area52.5
[APPROVEDAMT] => 68850.00
)
[1] => stdClass Object
(
[CUSTOMERTRXLINEID] => 2683960
[LINETYPE] => LINE
[LINEAMOUNT] => 10
[TAXAMOUNT] => 0
[INVOICEAMOUNT] => 153020
[CREDITEDAMOUNT] =>
[ORIGINALLINEAMOUNT] => 10
[DESCRIPTION] => Knowledge Fees (As per Government of Dubai - Law(4) 2005
[APPROVEDAMT] => 10
)
[2] => stdClass Object
(
[CUSTOMERTRXLINEID] => 2683961
[LINETYPE] => LINE
[LINEAMOUNT] => 10
[TAXAMOUNT] => 0
[INVOICEAMOUNT] => 153020
[CREDITEDAMOUNT] =>
[ORIGINALLINEAMOUNT] => 10
[DESCRIPTION] => Innovation Fees (As per Government of Dubai - Law(20) 2015)
[APPROVEDAMT] => 10
)
)
这是我尝试过的。
$total_line_items=$formData['totallineitem']->value;
$decode_array = json_decode($formData['invoice']->value);
$formArray = array($formData);
$ap_array = [];
for($x=0; $x<$total_line_items; $x++){
$ap_array[] = $formArray[0]['approve_amount_'.$x]->value;
}
for($i=0; $i<$total_line_items;$i++)
{
$data_value=$formData['data'.$i]->value;
$approved_amt=$formData['approve_amount_'.$i]->value;
if($data_value)
{
foreach($decode_array->LINEDETAILS as $array){
$array->APPROVEDAMT = $ap_array[$i];
print_r($array->APPROVEDAMT);
}
}
}
答案 0 :(得分:0)
尝试此代码
foreach ($array1 as $key => $value) {
$array1[$key]['APPROVEDAMT'] = $array2[$key];
}
它将循环第一个数组,并使用第二个数组值添加一个新索引。希望有帮助