douhoujun9304 2019-05-04 17:34
浏览 153
已采纳

将数组插入MYSQL数据库中的单个字段

I am creating a SELECT drown down list, I would like to have it loaded in 1 of 2 ways.

First way is pulling data from database with ID, name, and Value - This way will be loaded from other pages.

Second way, I would like to load from a Single line in the database with Name and Value. But I want to have that value be loaded with an array.

How do I load that array into the database?

$name = 'name';
$value = array( 'red' => 'Red', 'blue' => 'Blue' );
$SQL = 'INSERT INTO table_name (name, value) VALUES ($name,$value);

I expect when I run the SELECT * from table_name WHERE name = "name" to use that array in the value field right away.


With what "RakeshJakhar" said, to use the implode and explode onto $value

$value = array( 'red' => 'Red', 'blue' => 'Blue' );

print_r( $value );
echo "<br />";
echo implode( ",", $value );
echo "<br />";
$implode = implode( ",", $value );
$explode = explode( ',', $implode );
print_r( $explode );

Results:

Array ( [red] => Red [blue] => Blue ) 

Red,Blue

Array ( [0] => Red [1] => Blue )
  • 写回答

1条回答 默认 最新

  • dongye9453 2019-05-04 18:10
    关注

    You can use json_encode and json_decode

    $value  = array( 'red' => 'Red', 'blue' => 'Blue' );
    $column = json_encode($value);
    

    So the inserted value {"red":"Red","blue":"Blue"}

    When you are fetching value use json_decode

    $values = json_decode($column,true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分