0

我有一个这样的循环(只是一个示例,缺少许多变量):

foreach($inserts as $insert) {

$insert_update = 'INSERT INTO etc.. SET etc..'; // returns the last inserted ID

$insertedIDs[] = array($tables[$tbl]['owner'] => $insert_update);
}

现在,正如您所见$insertedIDs[],所有新插入的 ID 都进入了数组。问题是在下一个$inserts循环中,我需要它$insertedIDs[]可用于循环的其他变量,这将需要获取最后插入的 ID。问题是在下一个循环中这个变量不能被识别并且它返回错误。

$insertedIDs[]在第一个循环之后,如何在每个下一个循环中使用?

我尝试$insertedIDs[]在之后宣布为全球,foreach但没有运气。

4

3 回答 3

0

Pretty easy actually... Try not to over-think it.

//create an empty array
var $insertedIds = array();

//now loop and add to the array
foreach( $inserts as $insert )
{
    $insert_update = 'INSERT INTO etc.. SET etc..'; // returns the last inserted ID
    $insertedIds[][$tables[$tbl]['owner']] = $insert_update;

    //or if you want the owners to be keys in the first level of the array...
    //$insertedIds[$tables[$tbl]['owner']] = $insert_update;
}

//output the contents of the array
echo '<pre>' . print_r($insertedIds, true) . '</pre>';
于 2013-07-18T10:31:20.910 回答
0

试试这个可能对你有帮助:

$insertedIDs=Array();
foreach($inserts as $insert) {

$insert_update = 'INSERT INTO etc.. SET etc..'; // returns the last inserted ID

$insertedIDs[] = array($tables[$tbl]['owner'] => $insert_update);
}

现在您可以访问 $insertedIDs

于 2013-07-18T10:13:22.473 回答
0
$insertedIDs = array();

foreach($inserts as $insert) {
    $insert_update = 'INSERT INTO etc.. SET etc..'; // returns the last inserted ID
    $insertedIDs[ $tables[$tbl]['owner'] ] = $insert_update;
}

print_r($insertedIDs);
于 2013-07-18T10:13:55.213 回答