[英]How insert array value in each index of another array in php?
I am having two arrays, in that i need to insert the each index of last key and value of another array keys, values in php. 我有两个数组,因为我需要插入最后一个键的每个索引和另一个数组键的值,即php中的值。 My sample arrays are given below. 我的示例数组如下。 I am using codeigniter framework. 我正在使用codeigniter框架。
First array: 第一个数组:
Array
(
[0] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Bacardi Rum
[quantity] => 1
[unit_price] => 25.00
[inv_discount] => 0.00
[salesman_id] => 25,27
)
[1] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Baileys
[quantity] => 1
[unit_price] => 15.00
[inv_discount] => 0.00
[salesman_id] => 28,29
)
)
Second array: 第二个数组:
Array
(
[0] => 140140,150150
[1] => 151151,05180518
)
And i need the o/p : 我需要o / p:
Array
(
[0] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Bacardi Rum
[quantity] => 1
[unit_price] => 25.00
[inv_discount] => 0.00
[salesman_id] => 25,27
[salesman] => 140140,150150
)
[1] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Baileys
[quantity] => 1
[unit_price] => 15.00
[inv_discount] => 0.00
[salesman_id] => 28,29
[salesman] => 151151,05180518
)
)
Can any one help me, give some ideas to solve this. 谁能帮我,提出一些解决方案。
In this case, you have an array of objects ( stdClass type ) and one other array only. 在这种情况下,您只有一个对象数组( stdClass type )和另一个数组。 Answering your question you just have to do the code as shown below. 回答您的问题,您只需执行如下所示的代码。
foreach ($secondArray as $key => $value) {
$firstArray[$key]->salesman = $value;
}
or 要么
foreach ($firstArray as $key => $object) {
$object->salesman = $firstArray[$key];
}
foreach($arrA as $key=>$val){
$arrA[$key]['salesman'] = $arrB[$key];
}
As long as both arrays will be same size, array_map
will be suitable: 只要两个数组的大小相同, array_map
将是合适的:
$resultArray = array_map(function ($rowA, $rowB) {
$rowA->salesman = $rowB;
return $rowA;
}, $firstArray, $secondArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.