使用嵌套的foreach从多个数组构建新数组

时间:2018-06-28 09:39:47

标签: php arrays

我对如何从这三个数组中创建一个新数组,然后将新数组中的值传递给函数以将值插入数据库中感到困惑和困惑。

3个数组:

flavourname Array ( 
    [0] => Red Sour Cherry Extract 
    [1] => Red Energy (Red Bull) 
)
flavourcompanyname Array ( 
    [0] => Amoretti 
    [1] => Bolsjehuset 
)
flavourpercent Array ( 
    [0] => 5 
    [1] => 2 
)

预期结果:

Array ( 
    [0] => Array ( 
        [flavourname] => Red Sour Cherry Extract 
        [flavourpercent] => 5 
        [flavourcompany] => Amoretti  
    ) 
    [1] => Array ( 
        [flavourname] => Red Energy (Red Bull) 
        [flavourpercent] => 2 
        [flavourcompany] => Bolsjehuset 
    )
)

我尝试过的最接近的代码:

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);

从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
    )
) 

您可以看到上面的数组根本不是我想要的。

然后,我想将新数组的值传递给此函数以插入数据库:

foreach($newarray as $new) {
    $this->addRecipeFlavours($lastID, trim($new['flavourname']), trim($new['flavourcompany']), $new['flavourpercent'], $usereid);
}

功能:

/**
    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 个答案:

答案 0 :(得分:4)

您可以使用array_map遍历数组。

$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>";

这将导致:

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

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

)

文档:array_map()

答案 1 :(得分:2)

尝试此代码

代替nested for loop使用其他两个数组键

<?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);
?>

输出

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

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

)

答案 2 :(得分:1)

为什么不只使用一个循环并一次遍历每个数组?

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