我有一个Mysql数据库,其中一个字段包含以逗号分隔的引号字符串,我需要将该字段转换为一个数组或数组,并在每个术语中添加一些将来将更新的信息,如下例所示。
领域价值:“个人、群体和群体”、“多样性和包容”、“保护”
结果它是一个或多个数组,如:
“部分”:{“术语”:“个人、群体和群体”、“定义”:“}”、{“术语”:“多样性和包容性”、“定义”:“}、{”、“保护”、“定义”:“},
谢谢
发布于 2021-12-18 01:38:33
您可以使用preg_split来获取数组中的项。
<?php
$str = '"Individuals, groups and populations","Diversity and inclusion","Protection"';
$pattern = '/\"*\"/';
$components = preg_split($pattern, $str,-1,PREG_SPLIT_NO_EMPTY);
$components = array_diff($components,array(','));
print_r($components);
$a = array();
foreach($components as $c){
$array = null;
$array['term'] = $c;
$array['definition'] = "";
array_push($a,json_encode($array));
}
print_r($a);
?>发布于 2021-12-18 00:44:53
尝试使用json解码将其转换为PHP变量。
https://stackoverflow.com/questions/70400158
复制相似问题