如何使用str_split()?

时间:2015-06-26 06:44:46

标签: php arrays while-loop

我目前在我的项目中工作,我的问题从这里开始:我正在插入一个表并正确插入但问题是它看起来如此混乱,因为在一个id中有很多参考数字它们只是用逗号分隔,我想要做的是使用str_split来分隔它......

这是我的代码:

$sql = "INSERT INTO transaction_detail (`transaction_id`,`ref_number`)
          VALUES ('$transaction_id','$ref_number') ";

    $query = $conn->query($sql);

我该怎么办?我也想使用while循环,因为我不知道ref_number的最大数量?有人能帮助我吗?谢谢:))

更新

id  transaction_id ref_number
1           12      12411235435
2           12      214354657
3           12      2153564657

我希望我的输出看起来像这样,因为我的当前输出看起来像这样。

id    transaction_id    ref_number
1     12                12411235435,214354657,2153564657

更新

$ref_array = explode(',' , $ref_number);
$po_array = explode(',' , $po_number);
$inv_array = explode(',' , $inv_number);
$asn_array = explode(',' , $asn_number);
$adj_array = explode(',' , $adj_number);
$amount_array = explode(',' , $amount);

// count the number of po,invoice,asn and adj
if(count($po_array) != count($ref_array) || count($inv_array) != count($ref_array) || count($asn_array) != count($ref_array) || count($adj_array) != count($ref_array) || count($ref_array) != count($amount_array)){

    foreach ($ref_array as $i => $ref_num){
        $po_num = isset($po_array[$i]) ? $po_array[$i] : '' ; //leave blank there is no $po_array[$i]
        $inv_num = isset($inv_array[$i]) ? $invoice_array[$i] : '';
        $asn_num = isset($asn_array[$i]) ? $asn_array[$i] : '' ;
        $adj_num = isset($adj_array[$i]) ? $adj_array[$i] : '' ;
        $amount_num = isset($amount_array[$i])? $amount_array[$i] : '';



        if(intval($ref_num) != 0 ){

            $conn->query ("INSERT INTO transaction_detail (`transaction_id`,`ref_number`,`po_number`,`inv_number`,`asn_number`,`adj_number`,`amount`)
                    VALUES ('$transaction_id','$ref_num','$po_num','$inv_num','$asn_num','$adj_num','$amount_num') " );
        }
    }  
}  

以下是错误:

  

[2015年6月29日04:20:59欧洲/柏林] PHP注意:未定义的偏移量:1   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[2015年6月29日04:20:59欧洲/柏林] PHP注意:未定义的偏移量:2   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[29-Jun-2015 04:21:11 Europe / Berlin] PHP注意:未定义的偏移量:1   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[2015年6月29日04:21:11欧洲/柏林] PHP注意:未定义的偏移量:2   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[29-Jun-2015 04:21:11 Europe / Berlin] PHP注意:未定义的偏移量:3   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[2015年6月29日04:21:11欧洲/柏林] PHP注意:未定义的偏移量:4   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[2015年6月29日04:21:11欧洲/柏林] PHP注意:未定义的偏移量:5   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

     

[2015年6月29日04:21:11欧洲/柏林] PHP注意:未定义的偏移量:6   在第83行的C:\ xampp \ htdocs \ WebService \ webservice_revised.php

1 个答案:

答案 0 :(得分:3)

使用explode()拆分以逗号分隔的参考号,并使用foreach循环播放。

foreach (explode(',', $ref_number) as $refnum) {
    $conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`)
                  VALUES ('$transaction_id','$refnum') ";
}

要包含采购订单编号,请将该变量分解为另一个数组,并使用foreach循环中的索引来访问它:

$transaction_id = '123456';
$po_number = '1,2';
$ref_number = '11,22,33,44,55,66';

$po_array = explode(',', $po_number);
$ref_array = explode(',', $ref_number);

foreach ($ref_array as $i => $refnum) {
    $ponum = isset($po_array[$i]) ? $po_array[$i] : '';
    if (intval($refnum) != 0) {
        $conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`)
                      VALUES ('$transaction_id','$refnum', '$ponum') ");
    }
}

我测试了上面的代码,它执行以下查询,没有来自PHP的错误或通知:

INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','11', '1') 
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','22', '2') 
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','33', '') 
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','44', '') 
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','55', '') 
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','66', '') 

如果您的参考编号也可能少于po编号,您可以使用此代码将更长的数组用于循环:

$po_array = explode(',', $po_number);
$ref_array = explode(',', $ref_number);
$limit = max(count($po_array), count($ref_array));

for ($i = 0; $i < $limit; $i++) {
    $ponum = isset($po_array[$i]) ? $po_array[$i] : '';
    $refnum = isset($ref_array[$i]) ? $ref_array[$i] : '';
    $conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`)
                    VALUES ('$transaction_id','$refnum', '$ponum') ");
}
相关问题