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 ?

2
  • 1
    Possible duplicate of Merge/flatten an array of arrays in JavaScript? Commented Jul 20, 2018 at 6:29
  • @vahdet NO it's not. We have different scenario, I have already studied that befire posting a question. Commented Jul 20, 2018 at 6:46

1 Answer 1

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

5 Comments

thanks for the input, but the result of this one is same as array_push.
@ramedju Are you using Laravel in this case?
@SimonLay Yes I am :) I'll check this out. EDIT : after adding that one, returned item is empty.
@ramedju Ah, I forgot something in code. Please add it. Using $nameCol saves the data after merging. See my answer.
@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.

Your Answer

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