How can i work with multi dimensional array in php

Much thanks @ahundiak this instruction is well detailed, i will run it on my local machine and hint you about its outcome, but so far i love the simplicity of your teaching

This worked perfectly, but i was warned against using loops that it can be very slow especially for long rows or data

That inserting them all in one query is more faster and preferable.

Allow me to introduce you to another common programming principle: avoid premature optimization.

At this point in time you are still learning the very basics of how to securely insert sql data. Being concerned about saving a few milliseconds when inserting thousands of records is pretty much a waste of time. Classic example of premature optimization and why it should be avoided.

By the time you reach a point where your apps are indeed processing that magnitude of data you will understand how to optimize. And more importantly, when to optimize.

(Except if you never learn because you’re avoiding it.)

Am kind of trapped in between opinions at what point then is it considered safe to optimize.

When i have 10000 rows of 5 columns to insert?

It’s not a question of safety but rather a question of where your time should be better spent. At this point, you are not actually understanding the code you are being given. As long as the example exactly matches what you need then you copy/paste and continue. This is known as Cargo Cult programming and will only take you so far.

I could indeed post a working example of how to insert say 100 records using a single execute. But you would have no idea what it is doing or how it works or how to apply it elsewhere. If you did then you would not be asking.

Plus, without some bench marking and profiling of your specific requirements, I’m not even sure that doing so would even improve overall performance.

If someone else disagrees and wants to provide a working example then great. But for me, this sort of optimization is just a distraction from what should be the main goal of learning the basics.

2 Likes

Am really impressed with the response, as a learner or as insignificant as optimization is it means am just okay using foreach loop.

Am not too much that a novice i have a perfect code that can insert thousands of rows of two or more columns with binding param using str_repeat() and flattened array but it took my hours to code

Thats why i was looking at foreach loop which takes my simple param binding and insert through looping

Thanks for your advice

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.