There's no need for your second for loop, you can access the values after
explode.
explode() your $data string by ,, loop over the results and explode() each result (if it is not empty) by space:
<?php
$data = "3 003.3.003.003.006 31500.0,3 003.3.003.003.003 76500.0,4 003.3.003.003.002 72000.0,5 003.3.003.003.004 153750.0,6 003.3.003.003.005 187500.0,3 003.3.003.003.001 46500.0,";
$exone = explode(",", $data);
foreach ( $exone as $x ) {
if ( !empty($x) ) {
$extwo = explode(" ", $x);
// var_dump($extwo);
$sql = "INSERT INTO tbl_test(qty, kode, price) VALUES('{$extwo[0]}','{$extwo[1]}','{$extwo[2]}');";
echo $sql . PHP_EOL;
}
}
Output:
INSERT INTO tbl_test(qty, kode, price) VALUES('3','003.3.003.003.006','31500.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('3','003.3.003.003.003','76500.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('4','003.3.003.003.002','72000.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('5','003.3.003.003.004','153750.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('6','003.3.003.003.005','187500.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('3','003.3.003.003.001','46500.0');
If you uncomment the line // var_dump($extwo); you'll see that $extwo is an array with your desired values. You can access them using $extwo[0], $extwo[1] ...
echoing your$sqlwon't do anything. Also, you're inserting the same value for qty, kode and price? What is the expected output and what do you get?