对不同的属性组有不同的属性数据进行按照属性组归纳宏观提取显示

比如你的CSV文件数据类型是:

Season:Spring, Summer, Fall;Fabric:Elastic Woven Satin;Neckline:One-Shoulder;Silhouette:Trumpet/Mermaid;Sleeve:Sleeveless;Embellishment:Ruffles;Waist:Natural
提取代码:
$group = array();
$tags = explode(';', file_get_contents('old.csv'));
foreach($tags as $tag){
	$tag = array_map('trim', explode(':', $tag));

	$value = explode(',', $tag[1]);
	foreach(array_map('trim', $value) as $v){
		$group[trim($tag[0])][$v] = 1;
	}
}
var_dump($group);
exit();
最终会按照属性组为单位输出:
array(7) {
  ["Season"]=>
  array(3) {
    ["Spring"]=>
    int(1)
    ["Summer"]=>
    int(1)
    ["Fall"]=>
    int(1)
  }
  ["Fabric"]=>
  array(1) {
    ["Elastic Woven Satin"]=>
    int(1)
  }
  ["Neckline"]=>
  array(1) {
    ["One-Shoulder"]=>
    int(1)
  }
  ["Silhouette"]=>
  array(1) {
    ["Trumpet/Mermaid"]=>
    int(1)
  }
  ["Sleeve"]=>
  array(1) {
    ["Sleeveless"]=>
    int(1)
  }
  ["Embellishment"]=>
  array(1) {
    ["Ruffles"]=>
    int(1)
  }
  ["Waist"]=>
  array(1) {
    ["Natural"]=>
    int(1)
  }
}

将结果保存为CSV文件:

foreach($group as $key => $value){
	file_put_contents('data.csv', $key.',"'.join(',', array_keys($value))."\"\r\n", FILE_APPEND);
}

你可能感兴趣的:(属性组,属性提取)