how to transform data from a database into an array indexed?
from:
$bb = mysql_query("SELECT kalori FROM bb_temp ORDER BY density DESC");
//field kalori contains=(16,12,23,13,9,10,18)
become :
Array
(
[0] => 16
[1] => 12
[2] => 23
[3] => 13
[4] => 9
[5] => 10
[6] => 18
)
Thank you...
database into an array indexed
Moderator: General Moderators
- pohontaman
- Forum Newbie
- Posts: 4
- Joined: Thu Nov 28, 2013 10:20 pm
- Location: Kinabalu, Malaysia
-
CoursesWeb
- Forum Newbie
- Posts: 18
- Joined: Mon Dec 09, 2013 10:52 am
Re: database into an array indexed
Hi
Try use the explode() function, example:
Try use the explode() function, example:
Code: Select all
$str = '16,12,23,13,9,10,18';
$ar_str = explode(',', $str);
// test
var_export($ar_str);
- pohontaman
- Forum Newbie
- Posts: 4
- Joined: Thu Nov 28, 2013 10:20 pm
- Location: Kinabalu, Malaysia
Re: database into an array indexed
thanks for replying.
but the contents of the field is retrieved from the database. not declared.
are there any other suggestions?
but the contents of the field is retrieved from the database. not declared.
are there any other suggestions?
-
CoursesWeb
- Forum Newbie
- Posts: 18
- Joined: Mon Dec 09, 2013 10:52 am
Re: database into an array indexed
It is easy to implement that example with data from database.
Try this (it selects one row only, for multile rows, delete "limit 1", and the numbers should be added into a two dimensional array):
Try this (it selects one row only, for multile rows, delete "limit 1", and the numbers should be added into a two dimensional array):
Code: Select all
$bb = mysql_query("SELECT kalori FROM bb_temp ORDER BY density DESC limit 1");
$arr = array();
while ($row = mysql_fetch_array($bb)){
$arr = explode(',', $row['kalori']);
}
// test
var_export($arr);
Re: database into an array indexed
That latest code is close, but don't use explode() and append to $arr by using $arr[]=...