Reputation: 89
I have array which has this values
$_logfields=["_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts"];
and I want to select fields from database
$datas = Logss::select ("_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts")->offset(10)->limit(10 )->get();
after query my goal is to have json data like this
$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"]];
so how can I do that json what I did is
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);
but what this return is some things like this which is not working with my code
[{"headName":"_id","0":{"headVal":"5df2194d2d81db5e845586ce"},"1":{"headVal":"5df2194d2d81db5e845586cf"},"2":{"headVal":"5df2194d2d81db5e845586d0"},"3":{"headVal":"5df2194d2d81db5e845586d1"},"4":{"headVal":"5df2194d2d81db5e845586d2"},"5":{"headVal":"5df2194d2d81db5e845586d3"},"6":{"headVal":"5df2194d2d81db5e845586d4"},"7":{"headVal":"5df2194d2d81db5e845586d5"},"8":{"headVal":"5df2194d2d81db5e845586d6"},"9":{"headVal":"5df2194d2d81db5e845586d7"}},{"headName":"time2","0":{"headVal":"Thu, 28 Nov 2019 16:18:10 +0000"},"1":{"headVal":"Thu, 28 Nov 2019 16:18:12 +0000"},"2":{"headVal":"Thu, 28 Nov 2019 16:18:12 +0000"},"3":{"headVal":"Thu, 28 Nov 2019 16:18:13 +0000"},"4":{"headVal":"Thu, 28 Nov 2019 16:18:13 +0000"},"5":{"headVal":"Thu, 28 Nov 2019 16:18:13 +0000"},"6":{"headVal":"Thu, 28 Nov 2019 16:18:14 +0000"},"7":{"headVal":"Thu, 28 Nov 2019 16:18:14 +0000"},"8":{"headVal":"Thu, 28 Nov 2019 16:18:14 +0000"},"9":{"headVal":"Thu, 28 Nov 2019 16:18:15 +0000"}},{"headName":"logtype","0":{"headVal":"Traffic LOGS"},"1":{"headVal":"Traffic LOGS"},"2":{"headVal":"Traffic LOGS"},"3":{"headVal":"Traffic LOGS"},"4":{"headVal":"Traffic LOGS"},"5":{"headVal":"Traffic LOGS"},"6":{"headVal":"Traffic LOGS"},"7":{"headVal":"Traffic LOGS"},"8":{"headVal":"Traffic LOGS"},"9":{"headVal":"Traffic LOGS"}},{"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":"trust"},"9":{"headVal":"tunnel"}},{"headName":"SourceIP","0":{"headVal":"172.16.10.252"},"1":{"headVal":"172.16.10.252"},"2":{"headVal":"172.16.10.252"},"3":{"headVal":"172.16.10.252"},"4":{"headVal":"172.16.10.252"},"5":{"headVal":"172.16.10.252"},"6":{"headVal":"172.16.10.252"},"7":{"headVal":"172.16.10.252"},"8":{"headVal":"10.10.3.150"},"9":{"headVal":"10.5.5.30"}},{"headName":"DestinationIP","0":{"headVal":"10.10.0.3"},"1":{"headVal":"10.10.0.23"},"2":{"headVal":"10.10.0.4"},"3":{"headVal":"10.10.0.26"},"4":{"headVal":"10.10.0.23"},"5":{"headVal":"10.10.0.4"},"6":{"headVal":"10.10.0.188"},"7":{"headVal":"10.10.0.4"},"8":{"headVal":"194.27.156.207"},"9":{"headVal":"10.10.0.144"}},{"headName":"Protocol","0":{"headVal":"icmp"},"1":{"headVal":"icmp"},"2":{"headVal":"udp"},"3":{"headVal":"icmp"},"4":{"headVal":"icmp"},"5":{"headVal":"icmp"},"6":{"headVal":"icmp"},"7":{"headVal":"icmp"},"8":{"headVal":"udp"},"9":{"headVal":"udp"}},{"headName":"SendBytes","0":{"headVal":"60"},"1":{"headVal":"60"},"2":{"headVal":"142"},"3":{"headVal":"60"},"4":{"headVal":"60"},"5":{"headVal":"60"},"6":{"headVal":"60"},"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":"1"},"9":{"headVal":"0"}}]
this is the data which is received from database
[{"_id":"5df2194d2d81db5e845586ce","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:10 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.3","SendBytes":"60","RcvPkts":"1","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586cf","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:12 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.23","SendBytes":"60","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d0","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:12 +0000","Protocol":"udp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.4","SendBytes":"142","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d1","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:13 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.26","SendBytes":"60","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d2","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:13 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.23","SendBytes":"60","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d3","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:13 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.4","SendBytes":"60","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d4","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:14 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.188","SendBytes":"60","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d5","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:14 +0000","Protocol":"icmp","SourceIP":"172.16.10.252","DestinationIP":"10.10.0.4","SendBytes":"60","RcvPkts":"0","SourceZone":"tunnel"},{"_id":"5df2194d2d81db5e845586d6","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:14 +0000","Protocol":"udp","SourceIP":"10.10.3.150","DestinationIP":"194.27.156.207","SendBytes":"76","RcvPkts":"1","SourceZone":"trust"},{"_id":"5df2194d2d81db5e845586d7","logtype":"Traffic LOGS","time2":"Thu, 28 Nov 2019 16:18:15 +0000","Protocol":"udp","SourceIP":"10.5.5.30","DestinationIP":"10.10.0.144","SendBytes":"142","RcvPkts":"0","SourceZone":"tunnel"}]
Upvotes: 4
Views: 111
Reputation: 212
Where $arr
is the result of the database query you can use map function of the laravel collection or this built-in array_map
function
$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];
Upvotes: 2
Reputation: 1795
Update your query please
$datas = Logss::select ("_id", "time2", "logtype", "SourceZone", "SourceIP", "DestinationIP", "Protocol", "SendBytes", "RcvPkts")->offset(10)->limit(10 )->get()->toArray();
then use the following code
$new_array = array();
foreach($matches as $key=>$value){
foreach($value as $k=>$v){
$new_array[$k][] = $v;
}
}
echo json_encode($new_array);die;
Upvotes: 2