dpde7365
2011-07-07 06:22
浏览 560
已采纳

在MySQL数据库中插入一个数组

I want to insert an array into my MySQL database in the format a,b,c.

 `var_dump($pvt_contacts)`

results

array(1) { [0]=> array(2) { [0]=> string(3) "102" [1]=> string(1) "3" } } 

I tried

`implode(',',$pvt_contacts)' 

but it echos Array not 102,3 as I expected . Please help me

图片转代码服务由CSDN问答提供 功能建议

我想以a,b,c格式将数组插入MySQL数据库。

 `var_dump($ pvt_contacts)`
   
 
 

结果

   array(1){[0] =>  array(2){[0] =>  string(3)“102”[1] =>  string(1)“3”}} 
   
 
 

我试过

 `implode(',',$  pvt_contacts)'
   
 
 

但它像我预期的那样回声数组不是102,3。 请帮帮我

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • donglan8256 2011-07-07 06:40
    已采纳

    If you just want 102,3, then you're off by one (your pointing to a parent array, and not the elements):

    implode(',',$pvt_contacts[0]);
    
    • If you're trying to store an array in a DB, I recommend serialize. (despite its prevalence in Drupal, ExpressionEngine, Wordpress, etc. this is not generally the best idea).
    • If you're trying to sent an array to js, I recommend json_encode.
    • If you're doing something else I need more information.
    已采纳该答案
    打赏 评论
  • doudun8705 2011-07-07 06:29

    It is suggested to use multiple rows to store the data, instead of storing the whole array into it. If you insist, here is the way:

    Convert your array to the following format:

    [key1]{value1}#[key2]{value2}#.... etc
    

    You can use explode to convert it back to array.

    打赏 评论
  • doukai1226 2011-07-07 06:32

    The easiest way should be either serialize() or var_export(). But you should only use those if you do not intend to use the data inside the database in any way. If you want to run e.g. SELECT queries on this array data you should store it in a normalized way.

    打赏 评论
  • dongpo1216 2011-07-07 06:36

    You should use serialize(), unserialize() or json_encode() , json_decode()

    That would make it easier for you to write , and retreive back original data without the need of parsing your stored data yourself . Here are some good links to assist you :

    Preferred method to store PHP arrays (json_encode vs serialize)

    http://php.net/manual/en/function.json-encode.php

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

    打赏 评论
  • dragon8837 2011-07-07 06:36

    You can implode your array into a string then explode it back. Default separator is the ,. If you want to run SELECT queries then you can use the MySQL-specific FIND_IN_SET() function however this whole scenario is not recommened, you should use a normalized way as others suggested above.

    打赏 评论
  • douxian6008 2011-07-07 06:39

    One way is to convert array to JSON. It will convert array to string and you will be able to store it.

    Anyway, I don't think that this it the right way how to deal with the problem. Say, for example, you would need to store blog entries and tags for them into database.

    How you do it know?

    Save blog entry and tag into one table, one row. You convert all entries tags and save all into database.

    How you should do it?

    Make two tables:

    • entries,
    • entry_tags;

    All info that's related to entry save into entries. For example: title, description. All tags save into entry_tags. In that table there are id (primary key), entry_id (ID that have relationships with entries ID) and tag that simply holds the name.

    I hope this will help you!

    打赏 评论

相关推荐 更多相似问题