0

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()) {

    }
}

3 Answers 3

4

You can use array_map to loop thru the arrays.

$flavourname = //Your array
$flavourcompanyname = //Your array
$flavourpercent = //Your array

$result = array_map(function($a, $b, $c){
    return array(
        'flavourname' => $a,
        'flavourcompany' => $b,
        'flavourpercent' => $c,
    );
}, $flavourname, $flavourcompanyname, $flavourpercent);

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

Array
(
    [0] => Array
        (
            [flavourname] => Red Sour Cherry Extract
            [flavourcompany] => Amoretti
            [flavourpercent] => 5
        )

    [1] => Array
        (
            [flavourname] => Red Energy (Red Bull)
            [flavourcompany] => Bolsjehuset
            [flavourpercent] => 2
        )

)

Doc: array_map()

Sign up to request clarification or add additional context in comments.

2 Comments

Your loop is wrong , it will take duplicate values .
What do you mean by duplicate values? This output of the code is the same with the expected output
2

try this code

instead of nested for loop use other two array key

<?php
$flavourname = array ( "Red Sour Cherry Extract",  "Red Energy (Red Bull)" );
$flavourcompanyname = array ( "Amoretti", "Bolsjehuset" );
$flavourpercent = array ( 5 , 2 );
echo "<pre>";
foreach($flavourname as $key => $value) {
    $newarray[] = array('flavourname' => $value, 'flavourpercent' => $flavourpercent[$key], 'flavourcompany' => $flavourcompanyname[$key]);
}
print_r($newarray);
?>

OUTPUT

Array
(
    [0] => Array
        (
            [flavourname] => Red Sour Cherry Extract
            [flavourpercent] => 5
            [flavourcompany] => Amoretti
        )

    [1] => Array
        (
            [flavourname] => Red Energy (Red Bull)
            [flavourpercent] => 2
            [flavourcompany] => Bolsjehuset
        )

)

6 Comments

Apart from these being the wrong way around, 'flavourpercent' => $flavourcompanyname[$key], 'flavourcompany' => $flavourpercent[$key] this works great, thank you very much, how could I test if flavourcompany is empty and assign a value?
what you say i cant understand how could I test if flavourcompany is empty and assign a value?
@CodeX use the isset function to see if it is set to a value
I tried foreach($newarray as $new) {$flavourcompany1 = trim($new['flavourcompany']); if($flavourcompany1 == '') { $flavourcompany1 == 'User Defined'; }} but no joy..
try this $flavourcompany1=!empty($flavourcompany1')?$flavourcompany1: 'User Defined'; turnory operator @CodeX
|
1

Why not just use one single loop and traverse through each array at once?

$mixed = array();
foreach($flavourname as $i => $name){
   $mixed[]=array(
       'flavourname' => $name,
       'flavourcompany' => $flavourcompanyname[$i],
       'flavourpercent' => $flavourpercent[$i]
   );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.