CodeIgniter Forums
Two arrays, one SQL insert - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Two arrays, one SQL insert (/thread-29021.html)



Two arrays, one SQL insert - El Forum - 03-28-2010

[eluser]Kemik[/eluser]
Hey guys,

I've got two arrays, one called descriptions and one called prices, both have the same number of entries.

I want to insert a new row into the database for each of the array entries.

E.g.
Descriptions array
Quote:[0] => item 1
[1] => item 2
[2] => item 3

Prices array
Quote:[0] => 9.95
[1] => 12.20
[2] => 10.00

Psuedo query
Quote:Foreach (entry) {
Insert into invoices ($descriptions, $prices)
}

Would generate
Quote:Insert into invoices ('item 1', '9.95')
Insert into invoices ('item 2', '12.20')
Insert into invoices ('item 3', '10.00')

How do I take those two arrays and basically merge them in to one so I can do the foreach?

Thanks


Two arrays, one SQL insert - El Forum - 03-28-2010

[eluser]richthegeek[/eluser]
Simple way to merge two arrays, using one as the keys:
Code:
foreach( $one as $key=>$value ) $three[ $value ] = $two[ $key ];

To do yours, though:
Code:
foreach( $one as $key=>$value ) insert_into_invoices( $value, $two[ $key ] );



Two arrays, one SQL insert - El Forum - 03-28-2010

[eluser]thdls55[/eluser]
Edited: double post


Two arrays, one SQL insert - El Forum - 03-28-2010

[eluser]Kemik[/eluser]
Perfect Rich! Forgot all about the ability to select the key from an array and google searching "merging array" brought up adding items to the bottom/top of arrays, not what I wanted.

Thanks Smile