Php 拉维合并阵列

Php 拉维合并阵列,php,arrays,json,laravel,Php,Arrays,Json,Laravel,我有一个数组,它有这个值 $_logfields=["_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts"]; 我想从数据库中选择字段 $datas = Logss::select ("_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol"

我有一个数组,它有这个值

$_logfields=["_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts"];
我想从数据库中选择字段

$datas = Logss::select ("_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts")->offset(10)->limit(10 )->get();
查询之后,我的目标是获得类似这样的json数据

$tdata = [{ headName: "_id", headVal: ["32432dsfsdf34ffds","345dsfdsfcxsddfsfds34"] },{ headName: "time2", headVal: ["2/1/2019","3/2/2019"] },{ headName: "logtype", headVal: ["traffic","url"] },{ headName: "SourceZone", headVal: ["trust","untrust"] },{ headName: "SourceIP", headVal: ["192.168.1.1","192.168.5.1"] },{ headName: "DestinationIP", headVal: ["10.10.0.5","10.10.0.20"] },{ headName: "Protocol", headVal: ["udp","ICP"] },{ headName: "SendBytes", headVal: ["30","40"] },{ headName: "RcvPkts", headVal: ["5","7"]];
那么我该怎么做呢 我所做的是

for($i=0; $i < count($_logfields); $i++){
    $headname = $_logfields[$i];
    $col[$i]['headName'] = $_logfields[$i];

    for($j=0; $j < count($datas); $j++){    
        $col[$i][$j]['headVal'] = $datas[$j][$headname];
    }
}
echo json_encode($col);
for($i=0;$i
但是这个返回是这样的,它不适用于我的代码

“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“1”:“3”:“2”:“2”:“3”:“2”:“2”:“2”:“2”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“海德瓦尔”前言“3”:“3”:“3”:“4”:“4”:“4”:“1”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“头”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“3”:“头”:“3”:“3”:“3”:“3”:“3”:“3”:“头”:“3”:“3”:“5 DF219“3”:“5 DF219“5”。”。”。”。“5 2009“5 219“5 DF219“5DF219“5DF219“5 219“5 219:{“headVal”:“5df2194d2d81db5e845586d5”},“8”:{“headVal”:“5df2194d2d81db5e845586d6”},“9”:{“headVal”:“5df2194d2d81db5e845586d7”},{“headName”:“time2”,“0”:{“headVal”:“Thu, 2019年11月28日16:18:10+0000“},“1”:{“headVal”:“Thu,2019年11月28日16:18:12 +0000“,”2“,”Thu,2019年11月28日16:18:12+0000“,”3“,”Thu,2019年11月28日16:18:13+0000“,”4“,”Thu,2019年11月28日16:18:13+0000“,”5“,”Thu,2019年11月28日16:18:13+0000“,”6“,”Thu,2019年11月28日16:18:14+0000“,”7“,”Thu,2019年11月28日16:18:18+0000“,”headVal“,”2019年11月28日星期四16:18:14+0000“},“9”:{“headVal”:“2019年11月28日星期四16:18:15+0000”},{“headName”:“logtype”,“0”:{“headVal”:“Traffic LOGS”},“1”:{“headVal”:“Traffic LOGS”},“2”:{“headVal”:“Traffic LOGS” 日志“}”,3:{“headVal”:“流量日志”},4:{“headVal”:“流量” 日志“}”,5:{“headVal”:“流量日志”},6:{“headVal”:“流量 日志“}”,7:{“headVal”:“流量日志”},8:{“headVal”:“流量 日志“}”,9:{“headVal”:“流量” 日志“},{”headName:“SourceZone”,“0:{”headVal:“tunnel”},“1:{”headVal:“tunnel”},“2:{”headVal:“tunnel”},“3:{”headVal:“tunnel”},“4:{”headVal:“tunnel”},“5:{”headVal:“tunnel”},“6:{”headVal:“tunnel”},“7:{”headVal:“tunnel”},“8:{”headVal:“信任”},“9:“headVal:“tunnel”:“tunnel”},“ip”{headVal:“headVal”},“headVal”},ip”},“headVal”},“headVal”},“16:{“3”3““““““,”3““““““,”3““““,”3““““,”3“““,”3“““,”3“““,”3““““,”3“““““,”人头瓦尔““:”172.16.10.10.10.10.10.252,”3“,”3”3“,”3”3“,”,3”3“,3“,3“3“,3“3““,3“,3““““,,3“3“““,,,3“3“““““““,,,,,,3“““““““,,,,,,,,”3”3 3““““““““““““““,,,,,,,”3”3““““““““““““,,,,,,,,,”3 3““““““““““““““““““““headVal”:“10.10.0.3”},“1”:{“headVal”:”10.10.0.10.10.10.10.0.10.0.23“},”2““,,3“,3“,,,3“,,,,3“,,,,3“,,,,,,,3“,3“,,,,,3“,,,,,,,,,,,,,,,,,,,,,3““,,,,,,,,,,,,,,,3“,,,3“,,,,,,,,,,3“,,,,,,,,,,,,,,,,3“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,{“headVal”:“icmp”},2:{“headVal”:“udp”},3:{”“icmp”6“,6““““““,”6”6““““““,”6““““““““,”icmp”7“““““,”icmp”7“““““““,”7“““,”7”7“““,”icmp”3“,”,4”4““““““““,,,,,,,,,,,,,,,,,,,,,,,,,,”,4”4““““““““““,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“““““““““,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“}”,7:{“headVal”:“60”},8“:{“headVal”:“76”},“9”:{“headVal”:“142”},{“headName”:“RcvPkts”,“0”:{“headVal”:“1”},“1”:{“headVal”:“0”},“2”:{“headVal”:“0”},“3”:{“headVal”:“0”},“4”:{“headVal”:“0”},“5”:{“headVal”:“0”},“6”:{“headVal”:“0”},“7”:“headVal”:“0”},“8”:“headVal”:“headVal”:“0”}

这是从数据库接收的数据

[{u id:“5df2194d2d81db5e845586ce”,“logtype:“流量日志”,“time2:“Thu,2019年11月28日16:18:10+0000”,“协议:”icmp”,“SourceIP:“172.16.10.252”,“DestinationIP:“10.10.0.3”,“SendBytes:“60”,“RcvPkts:“1”,“SourceZone:“tunnel”},{id:“5df2194d2d81db5e845586cf”,“logtype:“流量日志”,“time2:“Thu,2019年11月28日16:18:12+icmp:“协议”:”,SourceIP:“172.16.10.252”,“DestinationIP:“10.10.0.23”,“SendBytes:“60”,“RcvPkts:“0”,“SourceZone:“tunnel”},{U id:“5df2194d2d81db5e845586d0”,“logtype:“流量日志”,“time2:“Thu,2019年11月28日16:18:12+0000”,“协议:“udp”,“SourceIP:“172.16.10.10.252”,“DestinationIP:“10.10.0.4”,“SendinationIP:“142”,“RcvPkts:”0:“0”,“logtype:“0”,“SourceZone:“tunnel”{id:”“5df2194d2d81db5e845586d1”,“日志类型”:“流量日志”,“时间2”:“2019年11月28日星期四16:18:13+0000”,“协议”:“icmp”,“源IP”:“172.16.10.252”,“目的IP”:“10.10.0.26”,“发送字节”:“60”,“RcvPkts”:“0”,“源区”:“隧道”},{“id”:“5df2194d2d81db5e845586d2”,“日志类型”:“流量日志”,“时间2”:“2019年11月28日星期四16:18:13+0000”,“协议”:“icmp”SourceIP:“172.16.10.252”,“DestinationIP:“10.10.0.23”,“SendBytes:“60”,“RcvPkts:“0”,“SourceZone:“tunnel”},{U id:“5df2194d2d81db5e845586d3”,“logtype:“Traffic LOGS”,“time2:“Thu,2019年11月28日16:18:13+0000”,“Protocol:“icmp”,“SourceIP:“172.16.10.10.252”,“DestinationIP:“10.10.0.4”,“SendBytes:“60”,“RcvPkts:”0:“0”,“SourceZone:“tunnel”{id:“0:”5df2194d2d81db5e845586d4,“日志类型”:“流量日志”,“时间2”:“2019年11月28日星期四16:18:14+0000”,“协议”:“icmp”,“源IP”:“172.16.10.252”,“目的IP”:“10.10.0.188”,“发送字节”:“60”,“RcvPkts”:“0”,“源区”:“隧道”},{“id”:“5df2194d2d81db5e845586d5”,“日志类型”:“流量日志”,“时间2”:“2019 11月28日星期四16:18:14+0000”,“协议”icmp“,”SourceIP:“172.16.10.252”,“DestinationIP:“10.10.0.4”,“SendBytes:“60”,“RcvPkts:“0”,“SourceZone:“tunnel”},{U id:“5df2194d2d81db5e845586d6”,“logtype:“Traffic LOGS”,“time2:“Thu,2019年11月28日16:18:14+0000”,“Protocol:“udp”,“SourceIP:“10.10.3.150”,“DestinationIP:“194.27.156.207”,“SendBytes:”76”,“RcvPkts:“1”,“SourceZone:“trust”},”5df2194d2d81db5e845586d7,“日志类型”:“流量日志”,“时间2”:“2019年11月28日星期四16:18:15+0000”,“协议”:“udp”,“源IP”:“10.5.5.30”,“目的IP”:“10.10.0.144”,“发送字节”:“142”,“RcvPkts”:“0”,“源区”:“隧道”}]


请更新您的查询

   $datas = Logss::select ("_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts")->offset(10)->limit(10 )->get()->toArray();
然后使用以下代码

$new_array = array();
    foreach($matches as $key=>$value){
        foreach($value as $k=>$v){

            $new_array[$k][] = $v;
        }

    }
    echo json_encode($new_array);die;

其中,
$arr
是t
 $headval = ['headname' => '_id', 'headval' => array_map(function($a){return $a['_id'];},$arr)];
 $time2 = ['headname' => 'time2', 'headval' => array_map(function($a){return $a['time2'];},$arr)];
 $newArray = [$headval,$time2];