i have an array of std class $fee_str and an another std class $total_amount. I want to marge both array to get one another std class object.
Array
(
[0] => stdClass Object
(
[class] => 1
)
[1] => stdClass Object
(
[class] => 10
)
)
Array
(
[0] => stdClass Object
(
[fee_amount] => 8285
)
[1] => stdClass Object
(
[fee_amount] => 300
)
)
$fee_str=$this->fee_settings_model->load_fee_structure();
$i=0;
foreach ($fee_str as $key => $classes) {
$total_amount[$i]=$this->fee_settings_model->get_total_by_class($classes->class);
$i++;
}
$data=array_merge($fee_str,$total_amount);
echo "<pre>";
print_r($data);
exit;
i want to get results like this
Array
(
[0] => stdClass Object
(
[class] => 1
[fee_amount] => 8285
)
[1] => stdClass Object
(
[class] => 10
[fee_amount] => 300
)
)
Please find inline doc for explanation,
function multiArrayCombine($array1, $array2)
{
$temp = array_map(null, $array1, $array2); // switch index wise data on either side
foreach ($temp as $key => &$value) { // & to make changes at address of variable
$value = json_decode(json_encode($value), true); // convert object to array
$value = array_merge(...$value); // removing initial index and merge internally
}
return json_decode(json_encode($temp)); // convert back to object form
}
$arr = multiArrayCombine($arr, $arr1);
print_r($arr);