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?

7
  • 7
    What do you get if you var_dump($recordset->column_name); ? Commented Nov 22, 2010 at 19:02
  • Hello Stephen, i get this: "0,0,0,3000,7000,2000,1000,1000,0,0" Commented Nov 22, 2010 at 19:03
  • 5
    So you get exactly the string that the exploded array represents? Commented Nov 22, 2010 at 19:04
  • It seems according to var_dump($variable), PHP is doing the accurate explosion of the string into array.. Commented Nov 22, 2010 at 19:04
  • 2
    Echo your string before you explode() it. You may not be pulling what you expect from the db. Commented Nov 22, 2010 at 19:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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..
The short ones are being returned in the expected order by coincidence. You'll need to specify an order.
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!
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.
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?

2 Comments

I've verified the values to be in this order: 0,2000,0,3000,1000,7000,1000,0,0,0 using phpMyAdmin console...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.