0

Should be a nice easy one today but I can't find the answer anywhere.

I have a multidimensional array. It stores partid and quantity.

$pCombined[] = array ( $newName[$b], $newQty[$b] );

I want to use PHP join as suggested here: Passing an array to a query using a WHERE clause

I need to get more data from mySQL about the parts so I need the list of ids joined up as I don't like the idea of running one SQL query for each row.

I tried to use:

$ids = join(',', $pCombined);

That just gives array, array, array....e.t.c.

I tried a few obvious combinations of square brackets. What I guess I want is...

$ids = join(',', $pCombined[*][0]); (where * is all)

I think I might also be overcomplicating the whole thing and I could use foreach or a for loop and join manually I guess. Its not quite a basic shopping cart but the principals are similar. Any ideas how I can do this or perhaps push me in a different direction. Thanks.

Community
  • 1
  • 1
James Pitt
  • 451
  • 2
  • 8
  • 19

1 Answers1

1

to combine array elements can use the following

$elemens = array("one","two","there","four")

$exit = implode(',',$elements);

print_r($exit);//one,two,...

I hope you serve

Walter Caraza
  • 911
  • 1
  • 10
  • 19
  • I don't think this is really what I am after. I need to prepare them for SQL but the original array is multi-dimensional. Imagine $pCombined[x][y] where [x] is rows and [y] is columns. I need to join all the items in column 1. – James Pitt Nov 22 '12 at 08:48
  • Thanks for the use of implode though. I used a for loop to collect the column data into a new array and then used $ids = implode(',', $newArray); I'm convinced there must be a way to do this without using a loop to extract the data I want from the multidimensional array though. – James Pitt Nov 29 '12 at 12:10