[英]PHP - modify formatted array
I have a function for arrays that I insert in db.我有一个用于 arrays 的 function 插入到数据库中。
$data = array(
"This\tIs\tAn\tExample\tOne\tOf\tMy\tArrays\"
);
I formatted it correctly and given output is with:我正确格式化并给出 output 是:
$result = array_walk($data, function(&$item) { $item = '{ ' .str_replace("\t", ', ', $item) . ' }'; });
and the result of one of the rows that is inserted in db in JSON format is:并且以 JSON 格式插入到 db 中的行之一的结果是:
{ This, is, Example }, { One, Of, My, Arrays}
and all of them are formatted that way.并且所有这些都以这种方式格式化。
How can I bypass this to get desired format like:如何绕过它以获得所需的格式,例如:
{ This, is, Example, One, Of, My, Arrays}
If you want to convert this如果你想转换这个
$data = array(
"This\tIs\tAn\tExample\tOne\tOf\tMy\tArrays\"
);
to this对此
{ This, is, Example, One, Of, My, Arrays}
you can simply do it by你可以简单地做到这一点
$result = [];
foreach ($data as $index => $value) {
$result[] = implode(',', explode("\t", $value));
}
$result = '{' . implode(',', $result) . '}';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.