Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Oke, so as the title suggest I want to insert multiple values from a nested array in one database field.

Just to be clear, the array below is going to be in one record in my database not in multiple rows.

This is what my array looks like.

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"
  }
}

I have an array called "stars" inside a general array and want all values from that "stars" array in one field in my database. Called of course, stars, for easy matching and searching.

I've looked at codeigniters batch_insert function but it doesn't do want I need it to do.

I hope that someone can atleast point me in the right direction on how to proceed.

Thank you in Advance.


share|improve this question
add comment

2 Answers

You should read Insert array into MySQL database with PHP before.

An advice: To make what you wish, search for MongoDB.

share|improve this answer
 
I don't have a problem inserting an array in my database, that works fine, I only have some trouble with nested arrays. But thank you anyways. –  user1928075 Jul 1 '13 at 16:18
add comment

MySQL doesn't have an array field type. Using MySQL you'd need two tables in the following Style:

films
id | FilmName
1    Star Trek

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

You could take this further with a link table inbetween so that you only have one row for each actor regardless of how many films they have been in.

In CI you could then insert each "actors" row once you've inserted the film. If you're using something like InnoDB for the tables you could also do this with a transaction (http://ellislab.com/codeigniter/user-guide/database/transactions.html).

If this isn't possible (why wouldn't it be?) you should consider a NoSQL based database like MongoDB.

share|improve this answer
 
Thank you for your nicely illustrated answer. I need to rethink how my database is going to be structured. I now have an foreach loop that compresses all the actors of a movie in a string. But I think that your idea might be the better solution. ;) –  user1928075 Jul 1 '13 at 16:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.