加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

将PHP数组存储在单个SQL单元中

发布时间:2020-12-13 18:23:27 所属栏目:PHP教程 来源:网络整理
导读:我希望在我的SQL数据库中将项目作为数组或类似数据类型存储,并且无法找到关于该主题的令人满意的信息 在PHP中,信息将存储为成分[“$id”] = true或false,其中$id表示成分 所以对于普通面包(请注意,这仍然是伪代码,因为数据输入端尚未启动) //code that creat
我希望在我的SQL数据库中将项目作为数组或类似数据类型存储,并且无法找到关于该主题的令人满意的信息

在PHP中,信息将存储为成分[“$id”] = true或false,其中$id表示成分

所以对于普通面包(请注意,这仍然是伪代码,因为数据输入端尚未启动)

//code that creates array this bit is still theory and will be manually emulated in the db for the time being
    $id=1;
    while (isset ($_POST["ingredient part of form".$ID])){
        if ($_POST["ingredient".$id]==checked){
            $p["ingredient"][$id]=true;
        }
        else{
            $p["ingredient"][$id]=false
        }
    $id++;
    }

//the code that gets the values back for the ingredient name we will use a separate array ingredient_n[$id]
echo p[$prod_id]["item"].': '
$id=1
while (isset ($ingredient[$id])){
    if ($p[$prod_id]["ingredient"][$id]==true)
        if ($id!=1){
            echo ',';
        }
        echo $ingredient_n[$id];
    }
}
echo '.';

这应该产生类似的东西

‘普通面包:小麦面粉,水,盐,酵母.

我查看了ENUM和SET,但这会增加新的成分

根据主题判断(将PHP数组存储在单个SQL单元中),您应该对此进行序列化.有标准化的方法
$array["a"] = "Hello";
$array["b"] = "World";
$array["c"] = "!";

$str = serialize($array);

// The contents of $str
// a:3:{s:1:"a";s:5:"Hello";s:1:"b";s:5:"World";s:1:"c";s:1:"!";}

反向使用反序列化

$unserializedString = unserialize($str);

您可以将它用于数组和对象.

> http://php.net/manual/en/function.serialize.php

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读