php & mysql - loop through columns of a single row and passing values into array

12,719

Try this:

$result = mysql_query("...");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    $arr = array();
    foreach ($row as $k=>$v)
    {
        $features = explode("#", $v);
        $value = $features[1]; // get the specific language feature
        $arr[] = $value;
    }
    $specs = join(", " , $arr);
}
Share:
12,719
bikey77
Author by

bikey77

Updated on July 27, 2022

Comments

  • bikey77
    bikey77 almost 2 years

    I have a mysql table with columns id, f1, f2, f3, ..., f20 where id is productID and f1,...f20 are product features. Depending on each product, some might have all, none or only some columns filled.

    Each column holds a delimited string like a#b#c#d where a,b,c,d are values in different languages (a=english, b=french etc)

    I need to select a row by it's id, explode each column's value (f1,f2...) with '#' in order to get the language part I need and then pass the values to an array in order to use in my product spec page.

    How do I loop through the fetched row (i'm using $row = my_fetch_array) and put the exploded value into a one dimension array like $specs=('green', 'M', '100', 'kids'...) etc? PS:I know, is complicated but I cant come up with a better idea right now.

  • bikey77
    bikey77 over 12 years
    This worked right away, thanks! I didn't know about the join function, nice.
  • Prabs
    Prabs about 9 years
    Hello @swatkins...would you please help me in solving this stackoverflow.com/questions/28493201/… related to same issue