[英]Turn a string into an array, separated by double quotes and commas (“,”) in php
[英]Transform string with term enclosed in quotes and separated by commas into array of subarrays in PHP
我有一個 Mysql 數據庫,其中一個字段包含用逗號分隔的引號中的一串術語,我需要將該字段轉換為數組或 arrays 並向每個術語添加一些將來會更新的信息,如下例所示.
字段值:“個人、群體和人群”、“多樣性和包容性”、“保護”
結果它是一個數組或 arrays ,例如:
“部分”:[{“術語”:“個人、群體和群體”,“定義”:“”},{“術語”:“多樣性和包容性”,“定義”:“”},{“術語”: “保護”,“定義”:“”}],
謝謝
嘗試使用json-decode將其轉換為 PHP 變量。
您可以使用 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.