如何在 Vue.js 中将数组的数组转换为单个数组?在我的 php
后端,我有下面的代码从数据库中获取数据。我现在的问题是我不知道如何在我的 js
端将它们转换为单个数组。
PHP 端:
$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;
结果是这样的:
每个数组内部是这样的:
最后里面是这样的:
我可以对它做 for 循环以将其转换为单个数组吗?如何做?或者我可以立即搜索其中的项目吗?因为我尝试过使用这样的方式进行搜索,但我认为这只能搜索或在单个数组中工作(这就是我想将它们合并到一个数组中的原因):
Vue.js 端
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.
知道怎么做吗?
最佳答案
您可以使用 array_merge
而不是 array_push
。
我猜你正在使用 Laravel
,你必须在合并之前将 collection
转换为 array
,参见示例:
$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;
关于javascript - 在 Vue.js 中转换数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51435952/