开发者

Problem with explode() in PHP

开发者 https://www.devze.com 2023-01-26 22:34 出处:网络
I\'m having a problem with my explode function in PHP. I\'m pulling a string from the database as follows:

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?


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号