RanaHaroon
RanaHaroon

Reputation: 453

how to join array's to another array against the indexes PHP

it might be simple, i want to join or merge arrays and insert them to database accordingly. arrays structure is like:

$movies = array();
$songs = array();
$singers = array();

having array data through html dom parser

foreach($somevar as $val){
    $movies[] = $val; // 7 results
}

foreach($somevar as $val){
    $songs[] = $val; // 7 results
}

foreach($somevar as $val){
    $singers[] = $val; // 7 results
}

Now i am having 3 arrays indexed with [0] to [6] each. i want to insert them to database. how i can join each 1st values against the ascending order. DB table is like:

INSERT INTO tbl_name (movie_name,song_title,singer_name) VALUES('$movie', '$song','$singer')

i'm unable to figure out how to join different array's values accordingly. please advise, any help would be greatly appreciated.

Upvotes: 0

Views: 24

Answers (1)

Lauwrentius
Lauwrentius

Reputation: 503

If those those are array parallel arrays, you can try

$movies = array("movie1","movie2","movie3","movie4","movie5","movie6"); 
$songs = array("songs1","songs2","songs3","songs3","songs4","songs5","songs6");
$singers = array("singers1","singers2","singers3","singers4","singers5","singers6");

$query = '';
for($i=0; $i<count($movies); $i++){
    $query .= "INSERT INTO tbl_name (movie_name,song_title,singer_name) VALUES('$movies[$i]', '$songs[$i]','$singers[$i]');";
}

//=======do multi query execution here

Upvotes: 1

Related Questions