У меня есть 2 массива, оба массива имеют значение Director_id для совпадения
Я пытаюсь сопоставить ключ одного массива со значениями другого массива.
$invest_details = DB::table('directors_invest')
->join('directors', 'directors.id', '=', 'directors_invest.director_id')
->select('directors_invest.*', 'directors.name as directors_name')
->get();
##
$expense_details = DB::table('receipts')
->join('directors', 'directors.id', '=', 'receipts.director_id')
->join('account_type', 'account_type.id', '=', 'receipts.type')
->join('expense_type', 'expense_type.id', '=', 'receipts.exp_earn_type')
->select('receipts.*', 'directors.name as directors_name', 'account_type.name as account_name', 'expense_type.name as exp_name')
->get();
//dd($expense_details);
$director = DB::table('directors')->get();
$directors = [];
foreach ($director as $value) {
$directors[$value->id] = $value->name;
}
// Here I will map Director_id and total invest
$investor_total_by_id = [];
foreach ($expense_details as $exp) {
if (isset($investor_total_by_id[$exp->director_id])) {
$investor_total_by_id[$exp->director_id] += $exp->amount;
} else {
$investor_total_by_id[$exp->director_id] = $exp->amount;
}
}
dd($investor_total_by_id);
//this gives me out put
//
// array:2 [▼
// 1 => 7100.0
// 2 => 444.0
// ]
//** Match Convert Director_id to Director Name. so it will be
// Director1 => 7100
// Director2 => 440
$director_id_name_map = [];
foreach ($director as $value) {
foreach ($investor_total_by_id as $key => $id_value) {
if (key($investor_total_by_id) == $director[$value->id]) {
$director_id_name_map[$directors->name] = $investor_total_by_id[$id_value->amount];
}
}
}
//dd($investor_total_by_id);
dd($$director_id_name_map);
return view('Accounts/Investment/add')
->with('invest_details', $invest_details)
->with('expense_details', $expense_details)
->with('directors', $directors)
->with('investor_total_by_id', $investor_total_by_id)
;
}
см. Вывод 2 Array
вывод $ investor_total_by_id
array:2 [▼
1 => 7100.0
2 => 444.0
]
Выход $ $ Director
Collection {#531 ▼
#items: array:2 [▼
0 => {#533 ▼
+"id": 1
+"name": "Directror1"
+"phone": ""
+"email": ""
+"address": "hi"
+"created_at": "2019-04-18 13:10:04"
+"updated_at": "2019-04-18 07:10:04"
}
1 => {#534 ▼
+"id": 2
+"name": "Director2"
+"phone": ""
+"email": ""
+"address": "hi"
+"created_at": "2019-04-18 14:35:56"
+"updated_at": "2019-04-18 07:10:04"
}
]
}
Я хочу создать таблицу в Blade, где она будет
Director1 7100
Direcot2 440
таблица будет ниже
<table border=1>
<th> Director Name </th>
<th> Amount</th>
<td> Director1 <td>
<td>
<td> 7100 </td>
<td> Director2 <td>
<td>
<td> 440 </td>
<table>