简体   繁体   English

将用引号括起来并用逗号分隔的字符串转换为 PHP 中的子数组数组

[英]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.

相关问题 将字符串转换为数组,用php中的双引号和逗号(“,”)分隔 - Turn a string into an array, separated by double quotes and commas (“,”) in php PHP数组,单词之间用逗号分隔 - PHP array with words separated by commas 字符串,仅用逗号分隔到多维php数组的树 - String, tree separated only with commas to an multidimensional php array 使用引号中的逗号分隔逗号分隔的字符串中的电子邮件地址 - Splitting comma separated email addresses in a string with commas in quotes 在PHP中用单引号引起来的字符串中允许使用哪些转义序列? - Which escape sequences are allowed in a string enclosed in a pair of single quotes in PHP? 给定PHP中的数组,其中的内容是用逗号分隔的字符串,如何提取字符串的第一部分? - Given an array in PHP where the contents are strings separated by commas, how do I extract the first part of string? PHP解析逗号分隔的字符串,带双引号 - PHP Parse Comma Separated String with double quotes 在由逗号php分隔的字符串中的值中添加引号 - Add quotes to values in a string separated by a comma php PHP将数组拆分为带逗号的字符串 - php split array to string with commas 将引号中包含的字符串更改为双引号 - Change string enclosed in quotes to double quotes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM