user6632933
user6632933

Reputation:

Convert an array of arrays in Vue.js

How can I convert an array of arrays to a single array in Vue.js? In my php back-end, I have the code below that fetch data from database. My problem now is I don't know how to convert them in my js side to a single array.

PHP side:

$name = DB::raw("CONCAT(emp.first_name, ' ', emp.last_name) AS name");
$nameCol = [];
$usedDate = '2018-07';
$date = 'attendance.date';
$total  = 'attendance.total';

for($i = 1; $i<32; $i++){
    if($i<10) $i = '0'.$i;
        $names = DB::table('attendance')
                 ->leftJoin('m_emp','m_emp.emp_key','=','attendance.emp_key')
                 ->select($name,$date,$total)
                 ->where(DB::raw("(DATE_FORMAT(attendance.date,'%Y-%m-%d'))"), '=', $usedDate.'-'.$i)
                 ->get();
        array_push($nameCol,$names);
}
return (array)$nameCol;

Result is like this:

enter image description here

Inside of each array is like this:

enter image description here

And finally inside it is this:

enter image description here

Can I do for loop on it to transform it to a single array and how? Or can I right away search for an item inside it? Because I have tried search using a way like this but I think this only search or works in a single array (that's my reason why I want to merge them to one array):

Vue.js side

list.find( empName=> empName.name === 'John Doe')
//let's assume list is the variable that receives data returned from php
//result for this one is undefined.

Any idea how ?

Upvotes: 1

Views: 678

Answers (1)

Simon.Lay
Simon.Lay

Reputation: 258

You can use array_merge instead of array_push.

I guess you are using Laravel , you have to convert the collection to array before merging that, see the example :

$name = DB::raw("CONCAT(emp.first_name, ' ', emp.last_name) AS name");
$nameCol = [];
$usedDate = '2018-07';
$date = 'attendance.date';
$total  = 'attendance.total';

for($i = 1; $i<32; $i++){
    if($i<10) $i = '0'.$i;
        $names = DB::table('attendance')
                 ->leftJoin('m_emp','m_emp.emp_key','=','attendance.emp_key')
                 ->select($name,$date,$total)
                 ->where(DB::raw("(DATE_FORMAT(attendance.date,'%Y-%m-%d'))"), '=', $usedDate.'-'.$i)
                 ->get();
        // use array_merge
        $nameCol = array_merge($nameCol,$names->toArray());
}
return (array)$nameCol;

Upvotes: 1

Related Questions