gpt4 book ai didi

php - array_push 字符串,来自用于 sql 插入的 json_encode

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

你好,我想存储来自 json 编码的字符串以将其存储在 MySQL 中,但问题是数组没有返回任何内容。这是我的代码:

    $com = array("1-1","1-2","1-3");
$classcom=array();
for ($i=1; $i<(int)$totaleleve; $i++) {
$eleve=array();
foreach($com as $value)
{
array_push($eleve,'2');

}
$final="'".json_encode($eleve)."'";
array_push($classcom,$final);
echo $final;
echo $classcom;
}
echo $classcom;

供您引用,$totaleleve='20',我这样做 $final="'".json_encode($eleve)."'"; 这样我可以集中数组的字符串,以便能够将其插入到 SQL 语句中,如下所示:

$sql="INSERT INTO table VALUES (".explode(",", $classcom).")"
// so that it looks like something like this:
$sql="INSERT INTO table VALUES ('["2","2","2"]','["2","2","2"]','["2","2","2"]')

$final 给了我我想要的 '["2","2","2"]' ,但是当我执行 array_push( $classcom,$final);,它只是给了我一个空白数组:

Array

有人可以帮我吗?谢谢!

最佳答案

如果$totaleleve大于0,则数组不为空。只需使用 print_rvar_dump 即可显示其值。

print_r($classcom)

而不是echo $classcom

关于php - array_push 字符串,来自用于 sql 插入的 json_encode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40953390/

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