本文介绍了PHP& mysql-遍历单行的列并将值传递到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mysql表,列ID为f1,f2,f3,...,f20,其中id为productID,而f1,... f20为产品功能。视每个产品而定,有些产品可能会填满全部,无列或仅填充某些列。

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.

每个列都包含一个分隔字符串,如a#b#c#d,其中a,b ,c,d是不同语言的值(a =英语,b =法语等)

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)

我需要通过其ID选择一行,展开每列的值(f1 ,f2 ...)和'#'来获取我需要的语言部分,然后将值传递给数组以便在我的产品规格页面中使用。

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.

如何遍历提取的行(我使用$ row = my_fetch_array)并将分解后的值放入一维数组,如$ specs =('green','M','100','kids' ...)等等?
PS:我知道,这很复杂,但是我现在无法提出一个更好的主意。

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.

推荐答案

尝试一下:

$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);
}

这篇关于PHP& mysql-遍历单行的列并将值传递到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 06:58