为什么要写转换程序
最近在做旧版程序迁徙,旧的架构为惯例的 MVC 模式,新版架构全副改成 restful 架构。
因为改版数据是统一的,然而申请构造不统一,新版的申请全副以 json 模式提交,为了不便测试,之前始终都是间接在浏览器关上开发者工具,而后把申请内容复制过去,而后手动改成 json 模式,因为之前数据量比拟少,临时未发现测试时候不不便的状况,然而明天遇到了数据比拟多的状况,于是我想,为啥不写一段转换程序呢?
自身来说,程序的初衷应该是进步工作效率,这也是我之前短少反思的中央,因而写下这篇文章做个记录。
代码
废话不多说,间接上代码,有啥有余的中央,后续有工夫再做钻研。
<?php
// 文件路经
$f = './t.data';
$content = file_get_contents($f);
$rows = explode("\n", $content);
$data = [];
foreach ($rows as $row) {
// 宰割 key 和 value
$rowInfo = explode(':', $row);
$keyInfo = array_shift($rowInfo);
$value = trim(implode(':', $rowInfo));
$keyArray = getCharArray($keyInfo);
$keyItem = '';
$keys = [];
foreach ($keyArray as $char) {if ($char == '[') {$keys[] = $keyItem;
$keyItem = '';
} elseif ($char == ']') {continue;} else {$keyItem .= $char;}
}
$keys[] = $keyItem;
// 驼峰转下划线
$keys = array_map(function ($item) {return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $item));
}, $keys);
$firstKey = array_shift($keys);
if (empty($keys)) {$data[$firstKey] = $value;
} else {if (!isset($data[$firstKey])) {$data[$firstKey] = [];}
$item = &$data[$firstKey];
while (!empty($keys)) {$currentKey = array_shift($keys);
if (!isset($item[$currentKey])) {$item[$currentKey] = [];}
$item = &$item[$currentKey];
}
$item = $value;
unset($item);
}
}
echo json_encode($data, JSON_UNESCAPED_UNICODE);
/**
* 字符串转字符数组
*
* @param string $string
*
* @return array
*/
function getCharArray(string $string)
{$strlen = mb_strlen($string);
$i = 0;
$charArray = [];
while ($i < $strlen) {$charArray[] = mb_substr($string, $i, 1);
$i++;
}
return $charArray;
}
示例文件:
thumbnail: https://avatar-static.segmentfault.com/393/562/3935623170-1140000000142909
name: 101 新手上路
description: 面向老手开发者的问题集中营
sidebar: 欢送来到 SegmentFault 101 新手上路问答。如果你是开发老手,有各种各样的纳闷,但又胆怯提出来被讥笑,欢送在此发问。本区禁止讥嘲,请大家认真解答问题。admins[0][user_id]: 1030000000094051
admins[0][isNoticeSite]: 0
admins[0][isNoticeMail]: 0
admins[1][user_id]: 1030000000091305
admins[1][isNoticeSite]: 0
admins[1][isNoticeMail]: 0
admins[2][user_id]: 1030000000146026
admins[2][isNoticeSite]: 0
admins[2][isNoticeMail]: 0
admins[3][user_id]: 1030000000141721
admins[3][isNoticeSite]: 0
admins[3][isNoticeMail]: 0
admins[4][user_id]: 1030000000142354
admins[4][isNoticeSite]: 0
admins[4][isNoticeMail]: 0
admins[5][user_id]: 1030000000144141
admins[5][isNoticeSite]: 0
admins[5][isNoticeMail]: 0
admins[6][user_id]: 1030000000091353
admins[6][isNoticeSite]: 0
admins[6][isNoticeMail]: 0
输入 json
{
"thumbnail": "https:\/\/avatar-static.segmentfault.com\/393\/562\/3935623170-1140000000142909",
"name": "101 新手上路",
"description": "面向老手开发者的问题集中营",
"sidebar": "欢送来到 SegmentFault 101 新手上路问答。如果你是开发老手,有各种各样的纳闷,但又胆怯提出来被讥笑,欢送在此发问。本区禁止讥嘲,请大家认真解答问题。",
"admins": [
{
"user_id": "1030000000094051",
"is_notice_site": "0",
"is_notice_mail": "0"
},
{
"user_id": "1030000000091305",
"is_notice_site": "0",
"is_notice_mail": "0"
},
{
"user_id": "1030000000146026",
"is_notice_site": "0",
"is_notice_mail": "0"
},
{
"user_id": "1030000000141721",
"is_notice_site": "0",
"is_notice_mail": "0"
},
{
"user_id": "1030000000142354",
"is_notice_site": "0",
"is_notice_mail": "0"
},
{
"user_id": "1030000000144141",
"is_notice_site": "0",
"is_notice_mail": "0"
},
{
"user_id": "1030000000091353",
"is_notice_site": "0",
"is_notice_mail": "0"
}
]
}