products(prod_id, prod_name,...)
attribute_key(attr_key_id, attr_name)
attributes_values(attr_val_id, attr_key_id, value)
product_attr_values(id,product_id,attr_val_id)
我已将MYSQL结果定义为数组定义。问题是我必须这样做 将同一属性与同一产品的不同值连接起来 逗号(;)。但是我可以在没有连接它的情况下获得结果。帮助赞赏。
$shop = Array
(
"0" => Array
(
"id" => 9,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Author',
"value" => 'sabsyasachi das'
),
"1" => Array
(
"id" => 10,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Author',
"value" => 'Ela Datta'
),
"2" => Array
(
"id" => 11,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Editor',
"value" => 'Sathi Basu'
),
"3" => Array
(
"id" => 12,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Editor',
"value" => 'Radha Prasad Gupta'
),
"4" => Array
(
"id" => 13,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Place_of_Publication',
"value" => 'Panagarh, Burdwan, West Bengal'
),
"5" => Array
(
"id" => 14,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Author',
"value" => 'sabsyasachi das'
),
"6" => Array
(
"id" => 15,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Author',
"value" => 'Ela Datta'
),
"7" => Array
(
"id" => 16,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Translated_by',
"value" => 'partha dasgupta'
),
"8" => Array
(
"id" => 17,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Editor',
"value" => 'Bandana Mukhopadhay'
),
"9" => Array
(
"id" => 18,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Editor',
"value" => 'Sathi Basu'
),
"10" => Array
(
"id" => 19,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Translated_Title',
"value" => '(Second World Telugu Conference, 1981)'
),
"11" => Array
(
"id" => 20,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Place_of_Publication',
"value" => 'Hyderabad'
),
"12" => Array
(
"id" => 21,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Foreword',
"value" => 'Information on Government organisations of Art and Culture in Andhra Pradesh'
),
"13" => Array
(
"id" => 22,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Descriptive_Tags',
"value" => 'Conference, Language'
)
);
$groups = array();
foreach($shop as $key => $array){
//$type is not necessary, it's just for clarity below
$product = $array['prodname'];
if( !isset($groups[$product]) ){
$groups[$product] = array();
$groups[$product]['prod'] = $array['prodname'];
}
$groups[$product][$array["attribute_name"]] = $array['value'];
}
//then combine the groups into a master array
$out = array();
foreach($groups as $g){
$out[] = $g;
}
echo '<pre>'. print_r($out, true).'</pre>';
print "<pre>";
//print_r($groups);
print "</pre>";
答案 0 :(得分:2)
使用MySQL GROUP BY
和GROUP_CONCAT
函数。
SELECT prod_name, GROUP_CONCAT(CONCAT(attr_name, ': ', value) SEPARATOR ', ') AS attributes FROM [your tables and joins] GROUP BY prod_id