Welcome Guest, Not a member yet? Register   Sign In
Two arrays, one SQL insert

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.

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?


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

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

Edited: double post

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

Theme © iAndrew 2016 - Forum software by © MyBB