Codeigniter - 从数据库中的嵌套数组中插入多个值

时间:2013-06-30 13:55:28

标签: php arrays codeigniter nested

Oke,因为标题建议我想在一个数据库字段中插入嵌套数组中的多个值。

为了清楚起见,下面的数组将在我的数据库中的一条记录中,而不是多行。

这就是我的数组的样子。

array(5) {
  ["stars"]=>
  array(5) {
    [0]=>
    string(10) "Chris Pine"
    [1]=>
    string(14) "Zachary Quinto"
    [2]=>
    string(11) "Zoe Saldana"
    [3]=>
    string(10) "Karl Urban"
    [4]=>
    string(10) "Simon Pegg"
  }
  ["directors"]=>
  array(1) {
    [0]=>
    string(11) "J.J. Abrams"
  }
  ["writers"]=>
  array(4) {
    [0]=>
    string(12) "Roberto Orci"
    [1]=>
    string(13) "Alex Kurtzman"
    [2]=>
    string(14) "Damon Lindelof"
    [3]=>
    string(16) "Gene Roddenberry"
  }
  ["genres"]=>
  array(4) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(9) "Adventure"
    [2]=>
    string(6) "Sci-Fi"
    [3]=>
    string(8) "Thriller"
  }
  ["movie_data"]=>
  array(12) {
    ["imdb_id"]=>
    string(9) "tt1408101"
    ["slug"]=>
    string(23) "star-trek-into-darkness"
    ["title"]=>
    string(23) "Star Trek Into Darkness"
    ["imdb_rating"]=>
    string(3) "8.2"
    ["release_date"]=>
    string(11) "6 June 2013"
    ["runtime"]=>
    string(4) "132m"
    ["mpaa_rating"]=>
    string(5) "PG-13"
    ["storyline"]=>
    string(554) "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew."
    ["plot"]=>
    string(202) "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one man weapon of mass destruction."
    ["poster_large"]=>
    bool(false)
    ["poster"]=>
    string(97) "http://ia.media-imdb.com/images/M/MV5BMTk2NzczOTgxNF5BMl5BanBnXkFtZTcwODQ5ODczOQ@@._V1._SY500.jpg"
    ["trailer"]=>
    string(157) "http://www.youtube.com/v/QAEkuVgt6Aw&feature=youtube_gdata_player?color2=FBE9EC&hd=1&autoplay=1&showsearch=0&version=3&modestbranding=1&fs=1&iv_load_policy=3"
  }
}

我在一般数组中有一个名为“stars”的数组,并希望数据库中一个字段中的“stars”数组中的所有值。当然称为星星,以便于匹配和搜索。

我已经查看了codeigniters batch_insert函数,但它确实不需要我这样做。

我希望有人能够至少指出我如何继续前进的正确方向。

先谢谢你。


2 个答案:

答案 0 :(得分:0)

您之前应阅读Insert array into MySQL database with PHP

建议:要制作您想要的内容,请搜索MongoDB。

答案 1 :(得分:0)

MySQL没有数组字段类型。使用MySQL,您需要以下样式中的两个表:

films
id | FilmName
1    Star Trek

actors
id | FilmId | Name 
1    1        Chris Pine
2    1        Zachary Quinto
3    1        Zoe Saldana

你可以进一步使用中间的链接表,这样每个演员只有一行,无论他们有多少部电影。

在CI中,您可以在插入电影后插入每个“演员”行。如果你在表中使用InnoDB之类的东西,你也可以通过一个事务(http://ellislab.com/codeigniter/user-guide/database/transactions.html)来完成这个。

如果这是不可能的(为什么不可能?)你应该考虑像MongoDB这样基于NoSQL的数据库。