[SOLVED] - Implode 2 arrays into 1

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

[SOLVED] - Implode 2 arrays into 1

Post by iknownothing »

Hey Guys,

I am Posting 2 arrays, which have to go into the database in one column. What needs to happen is it needs to go in a certain order in the implode, eg.

Code: Select all

Array1[0],Array2[0],Array1[1],Array2[1],Array1[2],Array2[2]
array_merge() is close, but not exactly what I'm looking for.

Does anyone know how this could be done easily?
Last edited by iknownothing on Sun Sep 02, 2007 11:35 pm, edited 2 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

array_merge() in the order you want... i guess

i'm guessing you don't really need the arrays in that order, just the values? of which case sort() might do you some good.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

no, the array itself HAS to be in that order. Heres an example:

Code: Select all

Size:     Price:
(Array1)  (Array2)
2L        $100.00
1L        $50.00
500mL     $25.00
250mL     $12.50
so the array should be..

Code: Select all

2L,$100.00,1L,$50.00,500mL,$25.00,250mL,$12.50
See, each price value, HAS to stay with its size...
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

Solved, probably a bad habit, but this is how I did it...

Code: Select all

$sizea = array_combine($_POST['size'], $_POST['pricea']);
		foreach ($sizea as $key => $value){
		$sizeaa .= $key . "," . $value . ","; 
		}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Why not a nested array?

Code: Select all

$array = array(
   'size1' => 'price 1',
   'size2' => 'price 2'
);
Then you could serialize() the array and store it in your database.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply