vendredi 24 novembre 2017

Getting a an array from for loop and use another for loop with that array in PHP

I have a for loop, and will form two arrays in the loo

foreach ($data as $key => $value) {
........
........
$user_insert[] = [
        'keyy' => $value,
        'key' => $value,
        ....
        ...
        ...
    ];
$someArray1[] = [
        /*'user_id' => $insert_id,*/
        'key1' => $value1,
        'keyy' => $value,
        .......
        ........
    ];
}

the count of $user_insert[] array is 4, the count of $someArray1 is 15.

after this for loop, I need to insert $user_insert array data to the database and use that inserted_id to insert next array $someArray1

foreach($user_insert as $insert_user){
    $unique_user_insert = array_unique($insert_user);
    //dd($unique_user_insert);
    $insert_id = DB::table('users')->insertGetId($unique_user_insert);

    foreach ($someArray1 as $someArray) {
        $someArray['user_id'] = $insert_id;
        DB::table('table_name')->insert($someArray);
    }
}

So the problem here is the data in the second loop is inserting 60 times(4 * 15). I need to insert only 15 rows.

The data($someArray1) is coming from the first for loop, but I need to add a user_id to that array which I get after the insert operation in second for loop.

So how can i insert only 15 rows.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire