php - 引用每个数组值
问题描述
我有一个 PHP 数组,如下所示。我需要customkey
将数组的每个值都用双引号引起来。
该数组当前如下所示:
Array
(
[test] => Array
(
[enrolId] =>
[custom] => Array
(
[0] => 5:5:1-5BPOMK,5:6:Cutom Product1,5:4:Reports Test
)
[query] =>
)
)
输出应该是:
Array
(
[test] => Array
(
[enrolId] =>
[custom] => Array
(
[0] => "5:5:1-5BPOMK","5:6:Cutom Product1","5:4:Reports Test"
)
[query] =>
)
)
解决方案
让我们用array_map()试试这样
<?php
function quoted_string($n)
{
return '"'.$n.'"';
}
$data = '5:5:1-5BPOMK,5:6:Cutom Product1,5:4:Reports Test';
$array = explode(',',$data);
$expected = array_map("quoted_string", $array);
echo implode(',',$expected);
?>
输出:
"5:5:1-5BPOMK","5:6:Cutom Product1","5:4:Reports Test"
推荐阅读
- python - 在 f 字符串中循环作为嵌入的值
- prometheus - promQL API 端点
- javascript - JS:用键检查对象与 hasOwnProperty?
- java - 如何减少以下代码中的内存使用量?
- java - PDFbox 字体为空
- vb.net - VB.NET 触发 Datagridview 单元格单击按钮 CLick
- javascript - 如何在 console.log 中查看函数的输出?
- xml - 用于更新 XML 文件的 Powershell 脚本
- javascript - React TypeError:无法读取未定义的属性“nbaGames”
- java - 如何给面板内的列固定列宽?