php&;mysql-循环一行的列并将值传递到数组中
我有一个mysql表,列id为f1、f2、f3、…、f20,其中id为productID,f1、…、f20为产品特性。根据每种产品的不同,有些产品可能会填写全部、无或仅填写某些列 每列包含一个分隔字符串,如a#b#c#d,其中a、b、c、d是不同语言的值(a=英语、b=法语等) 我需要按id选择一行,用“#”分解每列的值(f1,f2…),以获得我需要的语言部分,然后将值传递给数组,以便在我的产品规格页面中使用 如何循环获取的行(我使用$row=my_fetch_数组)并将分解后的值放入一维数组,如$specs=('green'、'm'、'100'、'kids'…)等? 我知道,这很复杂,但我现在想不出更好的主意 试试这个:php&;mysql-循环一行的列并将值传递到数组中,php,mysql,arrays,Php,Mysql,Arrays,我有一个mysql表,列id为f1、f2、f3、…、f20,其中id为productID,f1、…、f20为产品特性。根据每种产品的不同,有些产品可能会填写全部、无或仅填写某些列 每列包含一个分隔字符串,如a#b#c#d,其中a、b、c、d是不同语言的值(a=英语、b=法语等) 我需要按id选择一行,用“#”分解每列的值(f1,f2…),以获得我需要的语言部分,然后将值传递给数组,以便在我的产品规格页面中使用 如何循环获取的行(我使用$row=my_fetch_数组)并将分解后的值放入一维数组,
$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);
}
不确定这是最好的方法,但您可以使用langs定义一个数组,然后通过lang访问结果
<?php
$langs=array('eng'=>0,'fr'=>1,'ger'=>2,'geek'=>3);
while ($row=mysql_fetch_assoc($result)) {
$specs=explode('#',$row['f1']);
$other=explode('#',$row['f2']);
...
}
//Get lang from cookie that you could set elsewhere
$lang=(isset($_COOKIE['lang']))?$_COOKIE['lang']:'eng';
echo $specs[$langs[$lang]];
?>
我如何理解您的问题的解决方案:
// Make a MySQL Connection
$sQuery = "SELECT f1,f2,... FROM table WHERE id = ...";
$oResult = mysql_query($sQuery) or die(mysql_error());
//Fetch assoc to use the column names.
$aRow = mysql_fetch_assoc($oResult);
//Prepare the product properties array
$aProductProperties = array("English"=>array(),"French"=>array(),"Dutch"=>array());
//Loop over all the columns in the row
foreach($aRow as $sColName=>$sColVal){
//Explde the column value
$aExplodedCol = explode("#",$sColVal);
//The code below could be nicer when turned into a looped that looped over every language,
//But that would make the code less readable
$aProductProperties['English'][$sColName] = $aExplodedCol[0];
$aProductProperties['French'][$sColName] = $aExplodedCol[1];
$aProductProperties['Dutch'][$sColName] = $aExplodedCol[2];
}
//Done, you should now have an array with all the product properties in every language
你能给我们一个完整的表格行的例子吗?目前我只有我的mysql表格和很多纸上的草图!请去看电影。CSV是用于Excel的。此功能立即生效,谢谢!我不知道join函数,nice。你好@swatkins…你能帮我解决这个问题吗