0

我有这个数组结果:

Array
(
    [homeclub] => Array
        (
            [0] => MAGA
            [1] => AGUI
            [2] => BRAV
            [3] => TIBU
        )

    [homeclub_annotation] => Array
        (
            [0] => 3
            [1] => 11
            [2] => 0
            [3] => 8
        )

    [game_inning] => Array
        (
            [0] => 9 inn
            [1] => 10 inn
            [2] => 1 inn
            [3] => 10 inn
        )

    [visitor] => Array
        (
            [0] => CARI
            [1] => LEON
            [2] => TIGR
            [3] => CARD
        )

    [visitor_annotation] => Array
        (
            [0] => 2
            [1] => 10
            [2] => 
            [3] => 10
        )

    [status] => Array
        (
            [0] => FIN
            [1] => FIN
            [2] => SUSP
            [3] => FIN
        )

)

我需要构建一个这样的句子(以每个子数组的位置 0 为例):

INSERT INTO tbl_games (teamA, teamA_annotation, teamB, teamB_annotation, game_date, game_time, game_place, game_status)
VALUES(`MAGA`,
       `3`,
       `9 inn`,
       `CARI`,
       `2`,
       NOW(),
       NULL,
       NULL,
       `FIN`);

如何为每个子阵列获得正确的位置?请注意,要构建的句子从子数组和同一位置获取值,我应该使用 foreach 还是 for?对此有什么帮助吗?

4

2 回答 2

0

您首先需要构建一个简单的数组来处理 sql 插入:

$youArrDefinition = array(..blah blah..);

$rowsToInsert = array();

$rowsToInsert = array();


foreach ( $youArrDefinition as $keyCol =>  $groupCols ) {
    $i = 0;
    foreach( $groupCols as $colVal ) {
        $rowsToInsert[$i][$keyCol] = $colVal;  
        $i++; 
   }

这应该会给你一个更好的数组来操作,比如:

array(
     array(
         'homeclub' => 'blah',
         'homeclub_annotation' => 'blah'
         // blah blah rest array
    ),
    array(
         'homeclub' => 'blah2',
         'homeclub_annotation' => 'blah2'
         // blah2 blah2 rest array2
    ),
    // rest of your array
);

这是一个演示

于 2013-10-27T15:07:46.500 回答
0

用这个,

<?php


$arr = Array
(
    'homeclub' => Array
        (
            0 => 'MAGA',
            1 => 'AGUI',
            2 => 'BRAV',
            3 => 'TIBU',
        ),

    'homeclub_annotation' => Array
        (
            0 => 3,
            1 => 11,
            2 => 0,
            3 => 8,
        ),

    'game_inning' => Array
        (
            0 => '9 inn',
            1 => '10 inn',
            2 => '1 inn',
            3 => '10 inn',
        ),

    'visitor' => Array
        (
            0 => 'CARI',
            1 => 'LEON',
            2 => 'TIGR',
            3 => 'CARD',
        ),

    'visitor_annotation' => Array
        (
            0 => 2,
            1 => 10,
            2 => 12,
            3 => 10,
        ),

    'status' => Array
        (
            0 => 'FIN',
            1 => 'FIN',
            2 => 'SUSP',
            3 => 'FIN',
        )

);

for($int = 0; $int < count($arr['homeclub']); $int++){


    $query = 'INSERT INTO tbl_games (teamA, teamA_annotation, teamB, teamB_annotation, game_date, game_status, game_time, game_place) VALUES(';

     foreach($arr AS $key => $val){
        $query .= (is_integer($val[$int])) ? $val[$int] . ', ' : '\'' . stripslashes($val[$int]) . '\', '; 
     }

    $query .= 'NOW(), NULL, NULL);';

    echo $query; // Complete query string, run this with your chosen DB handler.
}

?>

将上面的回声留在原地,您将获得以下...

插入 tbl_games (teamA, teamA_annotation, teamB, teamB_annotation, game_date, game_status, game_time, game_place) VALUES('MAGA', 3, '9 inn', 'CARI', 2, 'FIN', NOW(), NULL, NULL );

插入 tbl_games (teamA, teamA_annotation, teamB, teamB_annotation, game_date, game_status, game_time, game_place) VALUES('AGUI', 11, '10 inn', 'LEON', 10, 'FIN', NOW(), NULL, NULL );

插入 tbl_games (teamA, teamA_annotation, teamB, teamB_annotation, game_date, game_status, game_time, game_place) VALUES('BRAV', 0, '1 inn', 'TIGR', 12, 'SUSP', NOW(), NULL, NULL );

插入 tbl_games (teamA, teamA_annotation, teamB, teamB_annotation, game_date, game_status, game_time, game_place) VALUES('TIBU', 8, '10 inn', 'CARD', 10, 'FIN', NOW(), NULL, NULL );

于 2013-10-27T16:23:23.023 回答