I'm stuck and confused on how I can create a new array from these 3 arrays and then pass the values from the new array to a function to insert the values into a Database.
The 3 Arrays:
flavourname Array (
[0] => Red Sour Cherry Extract
[1] => Red Energy (Red Bull)
)
flavourcompanyname Array (
[0] => Amoretti
[1] => Bolsjehuset
)
flavourpercent Array (
[0] => 5
[1] => 2
)
Expected result:
Array (
[0] => Array (
[flavourname] => Red Sour Cherry Extract
[flavourpercent] => 5
[flavourcompany] => Amoretti
)
[1] => Array (
[flavourname] => Red Energy (Red Bull)
[flavourpercent] => 2
[flavourcompany] => Bolsjehuset
)
)
The Code I've tried that is the closest:
foreach($flavourname as $key => $value) {
foreach($flavourpercent as $key2 => $value2) {
foreach($flavourcompanyname as $key3 => $value3) {
$newarray[] = array(
'flavourname' => $value,
'flavourpercent' => $value2,
'flavourcompany' => $value3
);
}
}
}
print_r($newarray);
OUTPUT from print_r():
Array (
[0] => Array (
[flavourname] => Red Sour Cherry Extract
[flavourpercent] => 5
[flavourcompany] => Amoretti
)
[1] => Array (
[flavourname] => Red Sour Cherry Extract
[flavourpercent] => 5
[flavourcompany] => Bolsjehuset
)
[2] => Array (
[flavourname] => Red Sour Cherry Extract
[flavourpercent] => 2
[flavourcompany] => Amoretti
)
[3] => Array (
[flavourname] => Red Sour Cherry Extract
[flavourpercent] => 2
[flavourcompany] => Bolsjehuset
)
[4] => Array (
[flavourname] => Red Energy (Red Bull)
[flavourpercent] => 5
[flavourcompany] => Amoretti
)
[5] => Array (
[flavourname] => Red Energy (Red Bull)
[flavourpercent] => 5
[flavourcompany] => Bolsjehuset
)
[6] => Array (
[flavourname] => Red Energy (Red Bull)
[flavourpercent] => 2
[flavourcompany] => Amoretti
)
[7] => Array (
[flavourname] => Red Energy (Red Bull)
[flavourpercent] => 2
[flavourcompany] => Bolsjehuset
)
)
As you can see the above array is not what I want at all.
Then I want to pass the values of the new array to this function to insert into the DB:
foreach($newarray as $new) {
$this->addRecipeFlavours($lastID, trim($new['flavourname']), trim($new['flavourcompany']), $new['flavourpercent'], $usereid);
}
The Function:
/**
ADD FLAVOURS
*/
public function addRecipeFlavours($recipeID, $recipeFlavourName, $recipeFlavourCompanyName, $recipePercent, $usereid) {
$query = 'INSERT INTO recipe_flavours (recipe_flavour_name, recipe_flavour_company_name, recipe_flavour_percent, recipe_flavour_recipe_id, recipe_flavour_ueid) VALUES (:recipeflavourname, :recipeflavourcompanyname, :recipeflavourpercent, :recipeflavourid, :usereid)';
$stmt = $this->queryIt($query);
$stmt = $this->bind(':recipeflavourname',$recipeFlavourName);
$stmt = $this->bind(':recipeflavourcompanyname',$recipeFlavourCompanyName);
$stmt = $this->bind(':recipeflavourpercent',$recipePercent);
$stmt = $this->bind(':recipeflavourid',$recipeID);
$stmt = $this->bind(':usereid',$usereid);
if($this->execute()) {
}
}