如何将数组键名称与另一个数组键名称匹配

How to match Array Key name with another Array Key name

我有2个Array,两个Array的共同点是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个数组的输出

$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 中创建一个 table

Director1 7100
Direcot2  440

table 将低于

<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>

你需要遍历directors数组,然后在循环的路上,你可以通过director_id的帮助握手遇到$investor_total_by_id,然后从中提取数据,如下所示。

<table border="1">
    <tr>
        <th>Director Name</th>
        <th>Amount</th>
    </tr>
    @foreach ($directors as $key => $value)
        <tr>
            <td>{{ $value->name }}</td>
            <td>{{ $investor_total_by_id[$value->id] }}</td>
        </tr>
    @endforeach
</table>

编辑

return view('foldername.viewname', compact('directors', 'investor_total_by_id'));

您可以像上面那样调用视图并压缩两个数组。

编辑 2

$director_id_name_map = [];
foreach ($directors as $key => $value) {
    $director_id_name_map[$value] = $investor_total_by_id[$key];
}

通过上面的代码,您可以在控制器中映射您的期望。

compact — 创建包含变量及其值的数组

好的,根据您的意见,我正在考虑以下作为您的数据库 tables。

director_name(id,name)

receipts(id,exp_earn_type,amount,director_id)

  • 所以,首先我们内部加入两个 tables 并使用 sum().

    得到每位董事的总费用
  • 然后我们将前面的子查询与 director_name table 进行内部连接,以获取导演姓名及其其他详细信息。

代码:

<?php

    $director_expenses = DB::table('receipts')
                         ->join('director_name','receipts.director_id','=','director_name.id')
                         ->select('receipts.director_id', DB::raw('sum(receipts.amount) as total_expense'))
                         ->groupBy('receipts.director_id');
    
    $director_details = DB::table('director_name')
                        ->joinSub($director_expenses, 'director_expenses', function ($join) {
                            $join->on('director_name.id', '=', 'director_expenses.director_id');
                        })
                        ->select('director_name.id','director_name.name','director_expenses.total_expense')
                        ->get();
    
    dd($director_details);