[英]Transform string with term enclosed in quotes and separated by commas into array of subarrays in PHP
I have a Mysql database with one field that contains a string of terms in quotes separated by commas, I need to turn that field into an array or arrays and add to each term some information that will be updated in the future, as the example below.我有一个 Mysql 数据库,其中一个字段包含用逗号分隔的引号中的一串术语,我需要将该字段转换为数组或 arrays 并向每个术语添加一些将来会更新的信息,如下例所示.
field value: "Individuals, groups and populations","Diversity and inclusion","Protection"字段值:“个人、群体和人群”、“多样性和包容性”、“保护”
Result it an array or arrays such:结果它是一个数组或 arrays ,例如:
"Partof": [ { "term": "Individuals, groups and populations", "definition": "" }, { "term": "Diversity and inclusion", "definition": "" }, { "term": "Protection", "definition": "" } ], “部分”:[{“术语”:“个人、群体和群体”,“定义”:“”},{“术语”:“多样性和包容性”,“定义”:“”},{“术语”: “保护”,“定义”:“”}],
Thank you谢谢
Try use json-decode to converts it into a PHP variable.尝试使用json-decode将其转换为 PHP 变量。
You can use preg_split to get the items in array.您可以使用 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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.