我有一個 Mysql 資料庫,其中一個欄位包含用逗號分隔的一串術語,我需要將該欄位轉換為一個或多個陣列,并向每個術語添加一些將來會更新的資訊,如下例所示.
欄位值:“個人、群體和人群”、“多樣性和包容性”、“保護”
結果它是一個或多個陣列,例如:
"Partof": [ { "term": "Individuals, Groups and Population", "definition": "" }, { "term": "多樣性和包容性", "definition": "" }, { "term": "保護", "定義": "" } ],
謝謝
uj5u.com熱心網友回復:
您可以使用 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);
?>
uj5u.com熱心網友回復:
嘗試使用json-decode將其轉換為 PHP 變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/384963.html
