2

I'm having a problem with my explode function in PHP.

I'm pulling a string from the database as follows:

  column_name
  0,2000,0,3000,1000,7000,1000,0,0,0

After pulling this into an object called $recordset i'm using the explode function to make an array out of it... as follows:

  $array = explode(",",$recordset->column_name)

But some how, the array is not as what i would expect...

This is what i get when i echo the array:

     Array
     (
     [0] => 0
     [1] => 0
     [2] => 0
     [3] => 3000
     [4] => 7000
     [5] => 2000
     [6] => 1000
     [7] => 1000
     [8] => 0
     [9] => 0
     )

As you can see, i'm not getting the values as i should... However, if my string from the database is short, say:

    1000,0,1200,0

The above logic works fine..

I'm not sure how to debug or solve this problem..

Please, help?

Stephen
  • 18,827
  • 9
  • 60
  • 98
Jasdeep Singh
  • 3,276
  • 4
  • 28
  • 47

2 Answers2

5

The problem is not with explode(). The problem is the string you are pulling from the database. If this string is concatenated somehow, I would start looking there. If not, verify the string in your database, or verify the query that accesses the table.

Take a look at the documentation for GROUP_CONCAT. You can specify the order in the syntax.

Stephen
  • 18,827
  • 9
  • 60
  • 98
  • Yes, i'm concatenating the strings using GROUP_CONCAT in my query... do you think that might be a problem? If yes, then why is this not a problem if the concatenated strings are short, say 4 or 5 comma seperated values.. – Jasdeep Singh Nov 22 '10 at 19:09
  • 3
    The short ones are being returned in the expected order by coincidence. You'll need to specify an order. – Stephen Nov 22 '10 at 19:13
  • i have two more columns which i'm concatenating.. How can i maintain the consistency so that when i explode the arrays from the other two columns, i can associate them using array_combine..?? I'm sure i would use ORDER BY here again? right? and if i'm ordering it, should i order all the three coloumns based on a single column? Thanks! – Jasdeep Singh Nov 22 '10 at 19:18
  • Yes. Any column will do. It depends on your preference. Are you trying to order them by their relative position in the table? You could use the primary key or auto increment field, although I would advise against it, as the order could be changed without your knowledge. I would order by a relevant order column like a date or an arbitrary column you create for the purpose of order. – Stephen Nov 22 '10 at 19:22
1

The problem isn't explode, as you can see in this codepad explode is working correctly.

Check the values coming from your DB, and ensure they are in the order you expect.

Edit: How is this value being generated in the DB? Is it a static value in a field, or is it being created from concatenation?

Neil Aitken
  • 7,856
  • 3
  • 41
  • 40
  • I've verified the values to be in this order: 0,2000,0,3000,1000,7000,1000,0,0,0 using phpMyAdmin console... – Jasdeep Singh Nov 22 '10 at 19:07
  • 1
    That's the order in which they're displayed in phpMyAdmin, but phpMyAdmin might be using different logic to obtain or arrange that output. At any rate, if you draw values from a database without specifying an order in your SQL, the data can arrive in any order the database likes. – Hammerite Nov 22 '10 at 21:55