Asked  8 Months ago    Answers:  5   Viewed   28 times

How can I insert a new item into an array on any position, for example in the middle of array?

 Answers

55

You may find this a little more intuitive. It only requires one function call to array_splice:

$original = array( 'a', 'b', 'c', 'd', 'e' );
$inserted = array( 'x' ); // not necessarily an array, see manual quote

array_splice( $original, 3, 0, $inserted ); // splice in at position 3
// $original is now a b c x d e

If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself, an object or NULL.

Wednesday, March 31, 2021
 
Magnanimity
answered 8 Months ago
29

Inserting 3000 rows is not a lot and it should not take too much time if you do it properly. You must remember that you should always use prepared statements. You can execute the same statement multiple times with different data. When you wrap the whole thing in a transaction it should be executed really fast.

// Start transaction
$mysqli->begin_transaction();

// prepared statement prepared once and executed multiple times
$insertStatement = $mysqli->prepare('INSERT INTO crbsms_queue(sender, phone, message, user_id, time_submitted) VALUES(?,?,?,?,?)');
$insertStatement->bind_param('sssss', $sender, $phone, $message, $user_id, $date);
foreach ($phones as $phone) {
    $insertStatement->execute();
}

// Save and end transaction
$mysqli->commit();

If this doesn't improve the performance then it means you have a problem somewhere else. You need to profile and debug your application to find where the issue comes from.

Side note: Remember to enable mysqli error reporting, otherwise your transaction might not behave properly.

Wednesday, March 31, 2021
 
innovation
answered 8 Months ago
46

My tables were InnoDB tables and when i changed my tables to MyISAM the insert worked fine. Well i have never encountered this problem before. Well that did the trick for the time being.

If i want to use InnoDB engine for transactions? How can i get php to be able to insert values in InnoDB table? Any one got any suggestion? And i am using WAMP server and the MySQL is version 5.5.24. And i did change the InnoDB conf in my.ini but that did not seem to work either?

Wednesday, March 31, 2021
 
sunshinejr
answered 8 Months ago
91

well, "(.*)" in your regex matches everything after it has found the beginning of a url. so if you're expecting multiple urls in one string, or coma separated or what not, they will all be bundled together.

You need to be able to split your input first, and then validate that each element is a valid url before you pick it up for insertion.

Wednesday, March 31, 2021
 
Jubair
answered 8 Months ago
15

If your arrays are both numerically indexed and have the same keys, you can loop over them like this:

for( $i = 0 ; $i < count( $servicetype ) ; $i++ ) {
  $amcinsert2=mysql_query("insert into amc_service_types (amc_service_id,service_type,service_amount) values('$id','" . $servicetype[$i] "','" . $serviceamt[$i] . "')");

}

Also, mysql_* functions are deprecated, you should replace them with either PDO or mysqli_* functions.

Wednesday, March 31, 2021
 
Trott
answered 8 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share