gpt4 book ai didi

php - 解析字符串并将其存储到数据库中

转载 作者:行者123 更新时间:2023-11-29 08:28:54 25 4
gpt4 key购买 nike

继续this question :

我有这个字符串:

$table_summary = '
[Category Name 1=>Title x:Description y,Title q:Description n,Title r:Description m,]

!£|£!

[Category Name 2=>Title z:Description a,Title j:Description k,Title p:Description f,]

!£|£!
';

如您所见,字符串中有一个分隔符 !£|£!,我有 2 个表:

products_spec_subjects
id product_id val

products_spec_details
id title description subject_id

我想将提到的字符串存储到这些表中,而类别名称将存储到第一个表中,标题和描述等详细信息将存储到第二个表中并链接使用 subject_id 作为外键到第一个表。

所以毕竟,提到的字符串应该存储到数据库中,如(e.x $product_id=12):

products_spec_subjects
1 12 Category Name 1
2 12 Category Name 2


products_spec_details
1 Title x Description y 1
2 Title q Description n 1
3 Title r Description m 1
4 Title z Description a 2
5 Title j Description k 2
6 Title p Description f 2

这是迄今为止我不完整的代码:

$technical_specifications_arr = explode('!£|£!', $table_summary);
unset($technical_specifications_arr[count($technical_specifications_arr) - 1]);
foreach($technical_specifications_arr as $main_value){
$i = 0;
$this_arr = explode('=>', $main_value);
foreach($this_arr as $value){
$cat_name = substr($this_arr[0], 1);
$details = substr($this_arr[1], 0, -1);
if($i == 0){
$tech_main_insert = mysql_query("INSERT INTO products_spec_subjects (product_id, val) VALUES ('$product_id', '$cat_name')") or die(mysql_error());
$this_main_id = mysql_insert_id($link);
}
$i++;
$another_arr = explode(',', $details);
unset($another_arr[count($another_arr) - 1]);
foreach($another_arr as $another_val){

$final = explode(':', $another_val);
foreach($final as $final_value){
// now I can't separate the titles and description here
}
}
}
}

感谢任何帮助。

最佳答案

您不需要迭代 $final 循环。只需提取您的值(value)观即可:

            foreach($another_arr as $another_val){  
$final = explode(':', $another_val);
// $final[0] = title
// $final[1] = desc
echo $final[0].' - '.$final[1]."<br />\n";
}

关于php - 解析字符串并将其存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com