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

[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
#2

[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 ] );
#3

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB