【问题标题】:Convert Column's data to multi column using laravel mysql (convert column's data to particular column )使用 laravel mysql 将列的数据转换为多列(将列的数据转换为特定列)
【发布时间】:2020-03-23 01:58:57
【问题描述】:

我有一个列名为 msg 的数据库,该列将保存这样的日志数据

time=13:43:17 devname="IYC-KIZ-YURDU" devid="FG100D3G12811704" logid="0000000013" type="traffic" subtype="forward" level="notice" vd="root" eventtime=1569408197 srcip=172.16.3.166 srcname="android-44713cd294193fd7" srcport=40583 srcintf="lan" srcintfrole="lan" dstip=40.90.137.125 dstport=443 dstintf="wan1" dstintfrole="wan" poluuid="f8fea7d4-f01a-51e8-6ad6-ed9b47f5b56c" sessionid=1226038 proto=6 action="server-rst" user="30853744318" group="Radius" authserver="DC_Radius" policyid=13 policytype="policy" service="HTTPS" dstcountry="United States" srccountry="Reserved" trandisp="snat" transip=192.168.100.32 transport=40583 appid=41475 app="Microsoft.Authentication" appcat="Collaboration" apprisk="elevated" applist="iyc_app" duration=246 sentbyte=2203 rcvdbyte=7189 sentpkt=19 rcvdpkt=19 wanin=6217 wanout=1207 lanin=1207 lanout=1207 utmaction="allow" countapp=1 sentdelta=0 rcvddelta=132 devtype="Android Phone" devcategory="Android Device" osname="Samsung Galaxy" osversion="Android 5.1.1" mastersrcmac="e4:5d:75:c4:d5:5b" srcmac="e4:5d:75:c4:d5:5b" srcserver=0 |       NULL |       NULL | NULL        | NULL      |          NULL |    NULL | NULL         

所以我想做的是将此列拆分为多列,空白在两列之间分开, 这是我从数据库中获取数据的代码

$draw = intval($request->input('draw'));
$start = intval($request->input('start'));
$length = intval($request->input('length'));
$order = $request->input('order');
$search=$request->input('search');
$search = $search['value'];
$col = 0;
$dir = "";
$limit = (int)$request->input('length');
$total_logs= $this->totalLogs();
$offset=$draw * $length;
$data=Logs::offset($offset)->limit($limit)->get();// DataTables::of(Logs::take($limit )->get(['msg', 'sys']))->make(true);

$output = array(
    "draw" => $draw,
    "recordsTotal" => $total_logs,
    "recordsFiltered" => $total_logs,
    "data" => $data
);

echo json_encode($output);

此代码用于服务器端数据表,但我想要将 msg 列分隔为单独的列并将其作为列发送到数据表

【问题讨论】:

  • 对不起,你想要什么?将其保存为 json 的一列或拆分并将每个字段放在单独的列中的某个表中?
  • 不不,我想用空白分割列的数据并获取此数据信息,例如 devname="IYC-KIZ-YURDU" 我想获取此值并将其添加到行的信息中跨度>

标签: php mysql laravel datatable


【解决方案1】:

如果我正确理解了您的问题,请尝试以下操作:

$pairs = explode(' ', $s); // explode by space
$res = []; // here will be results
foreach ($pairs as $pair) { // traverse all pairs
    $trimmed = trim($pair); // trim spaces
    if ( !$trimmed || 'NULL' === $trimmed || '|' === $trimmed ) {
        continue; // skip garbage
    }
    $data = explode('=', $pair); // split by '='
    $res[$data[0]] = isset($data[1]) ? str_replace('"', '', $data[1]) : ''; // put into result
}

然后在$res 中,您将拥有密钥对

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多