1

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 ?

  • 1
    Possible duplicate of [Merge/flatten an array of arrays in JavaScript?](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript) – vahdet Jul 20 '18 at 06:29
  • @vahdet NO it's not. We have different scenario, I have already studied that befire posting a question. –  Jul 20 '18 at 06:46

1 Answers1

1

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;
Simon.Lay
  • 263
  • 2
  • 9
  • thanks for the input, but the result of this one is same as `array_push`. –  Jul 20 '18 at 06:28
  • @ramedju Are you using Laravel in this case? – Simon.Lay Jul 20 '18 at 06:30
  • @SimonLay Yes I am :) I'll check this out. **EDIT :** after adding that one, returned item is empty. –  Jul 20 '18 at 06:45
  • @ramedju Ah, I forgot something in code. Please add it. Using $nameCol saves the data after merging. See my answer. – Simon.Lay Jul 20 '18 at 07:16
  • @SimonLay Apologies, I'm trying my own way here. Anyways, it works now. I can use my search code :) And maybe please upvote my question so that it may not be marked as duplicate by other users anymore. –  Jul 20 '18 at 07:34