如何使用str_split()?
How to use str_split ()?
我目前正在我的项目中工作,我的问题从这里开始:我正在插入 table 并且它正确插入但问题是它看起来很乱,因为在一个 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') " );
}
}
}
错误如下:
[29-Jun-2015 04:20:59 Europe/Berlin] PHP Notice: Undefined offset: 1
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:20:59 Europe/Berlin] PHP Notice: Undefined offset: 2
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 1
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 2
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 3
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 4
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 5
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 6
in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
使用 explode()
拆分以逗号分隔的参考编号,并使用 foreach
.
循环它们
foreach (explode(',', $ref_number) as $refnum) {
$conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`)
VALUES ('$transaction_id','$refnum') ";
}
要包含 PO 编号,将该变量分解为另一个数组,并使用 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') ");
}
我目前正在我的项目中工作,我的问题从这里开始:我正在插入 table 并且它正确插入但问题是它看起来很乱,因为在一个 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') " );
}
}
}
错误如下:
[29-Jun-2015 04:20:59 Europe/Berlin] PHP Notice: Undefined offset: 1 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:20:59 Europe/Berlin] PHP Notice: Undefined offset: 2 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 1 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 2 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 3 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 4 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 5 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 6 in C:\xampp\htdocs\WebService\webservice_revised.php on line 83
使用 explode()
拆分以逗号分隔的参考编号,并使用 foreach
.
foreach (explode(',', $ref_number) as $refnum) {
$conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`)
VALUES ('$transaction_id','$refnum') ";
}
要包含 PO 编号,将该变量分解为另一个数组,并使用 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') ");
}