I have function which fetches data from external database and I get specific film information such as id, name, genre etc. Now if movie genre is for example Animation I want only this one genre to be added to db. Now I can solve issue using isset but then I will have db entries for genres null and I want to avoid this. What is best way to approach this problem? Using foreach loop?
$records = array(
array('film_id' => $movies['id'], 'genre_id' => $movies['genre'][0]),
array('film_id' => $movies['id'], 'genre_id' => $movies['genre'][1]),
array('film_id' => $movies['id'], 'genre_id' => $movies['genre'][2])
);
$this->db->insert_batch('film_genre', $records);
You can also use array_filter
$arr = array(
array("id"=>3, "genre"=>"action"),
array("id"=>13, "genre"=>"comedy"),
array("id"=>23, "genre"=>"action"),
array("id"=>53, "genre"=>"comedy"),
array("id"=>63, "genre"=>"drama"),
array("id"=>73, "genre"=>"action"),
array("id"=>83, "genre"=>"drama"),
);
function genre($var)
{
if($var['genre'] == 'action')
{
return $var;
// you can also do the insert here like $this->db->insert();
}
}
$filtered = array_filter($arr, "genre");
print_r($filtered)
You could group your fetched data using Mysql's GROUP BY
function