4

I have 2 Array, both Array has common is Director_id to match

I am trying to map key of one Array with values of another array.

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

;
}

see the output of 2 Array

the output of $investor_total_by_id

array:2 [▼
  1 => 7100.0
  2 => 444.0
]

The output $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"
    }
  ]
}

I am looking to create a table in Blade where it will be

Director1 7100
Direcot2  440

the table will be below

<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>
2
  • You could do this with a query itself than achieving this with PHP. Can you explain your queries and what are you trying to do? Commented Apr 19, 2019 at 5:44
  • @vivek_23 , Thanks, Yes, thats what I was thiking, there might be a easy way by using SQL query, I am using Laravel, basically , I have 2 Table, director_name(id,name), Receipts ( id,exp_earn_type,amount,diretor_id) , I am trying achive, Which Director has expense how much , Currenlty i can get this as Director Id and amount, just need to map it by Director name and amount . Commented Apr 19, 2019 at 5:55

2 Answers 2

2

Ok, so based on your comments, I am considering below as your DB tables.

director_name(id,name)

receipts(id,exp_earn_type,amount,director_id)

  • So, first we inner join both tables and get the total expense of each director using sum().

  • Then we inner join the previous subquery with director_name table to get the director names with their other details as well.

Code:

<?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);
Sign up to request clarification or add additional context in comments.

Comments

1

You need to traverse directors array and then on the way of looping, you can meet $investor_total_by_id by doing handshake with the help of director_id and then extracting data from it like below.

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

EDIT

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

You can call view like above along with compacting both the arrays.

EDIT 2

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

By Above code you can map your expectations in controller.

compact — Create array containing variables and their values

7 Comments

thanks I am writting into Controller, then from Controller i will need to pass the values to blade, in Conroller, how will i map id from director Array ( which comming from table) and investor_total_by_id array ` $director_id_name_map=[]; foreach ($director as $key => $value) { // How do i map id from director Array with id with $investor_total_by_id ? } `
@alammd Once check my EDIT 2 portion of answer.
Please see the content of $director in the above question, just to point it out, i am using varaible $director which comming from database, not $directors[]. ( appologis for bad namming variable) so when i use your code it say , "Undefined offset: 2"
Ohh, okay, changed complete code of edit 2 portion, once refresh this page and check again.
$director_id_name_map[$value] = $investor_total_by_id[$key]; check this now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.