Page 1 of 1
database into an array indexed
Posted: Thu Dec 12, 2013 1:00 am
by pohontaman
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...
Re: database into an array indexed
Posted: Thu Dec 12, 2013 1:28 am
by CoursesWeb
Hi
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);
Re: database into an array indexed
Posted: Thu Dec 12, 2013 1:39 am
by pohontaman
thanks for replying.
but the contents of the field is retrieved from the database. not declared.
are there any other suggestions?
Re: database into an array indexed
Posted: Thu Dec 12, 2013 2:08 am
by CoursesWeb
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):
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
Posted: Thu Dec 12, 2013 2:29 am
by requinix
That latest code is close, but don't use explode() and append to $arr by using $arr[]=...