这可行,但比地狱更难,基本上它是在子数组的两个独立部分中迭代,看看两个子数组的值除了1之外是否有最大公分母,如果存在,则将基值乘以1.5
提前抱歉草率的代码。
error_reporting(E_ALL);
ini_set('display_errors', '1');
class CSVParser
{
public $output = NULL;
public $digits = NULL;
public function __construct($file)
{
if (!file_exists($file)) {
throw new Exception("$file does not exist");
}
$this->contents = file_get_contents($file);
$this->output = array();
$this->digits = array();
$this->factor = array();
}
public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n")
{
$lines = explode($newlineChar, $this->contents);
foreach ($lines as $line) {
if (strlen($line) == 0) continue;
$group = array();
list($part1, $part2) = explode($separatorChar2, $line);
$group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t"));
$group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t"));
$this->output[] = $group;
}
}
private function trim_value($value, $chars)
{
return preg_replace("#^( |" . $chars . ")+#", '', $value);
}
private function gcd($x,$y)
{
do {
$rest=$x%$y;
$x=$y;
$y=$rest;
} while($rest!==0);
return $x;
}
public function algorithm()
{
$alpha = array(
'c' => str_split('bcdfghjklmnpqrstvwxz'),
'v' => str_split('aeiouy')
);
$i=$k=0;
foreach ($this->output as $item) {
$cnt = 0;
$this->digits[$i] = array();
foreach ($item as $part) {
$this->digits[$i][$cnt] = array();
$new = array();
foreach ($part as $str) {
$v = count(array_intersect(str_split($str), $alpha['v']));
$c = count(array_intersect(str_split($str), $alpha['c']));
$t = strlen(str_replace(' ', '', $str));
$new = ($cnt == 0)
? array('v' => $v, 'c' => $c, 't' => $t, 'm' => ($t%2) ? $v * 1.5 : $c)
: array('v' => $v, 'c' => $c, 't' => $t);
$this->digits[$i][$cnt][] = $new;
}
$cnt++;
}
$i++;
}
$h=$cuml=0;
foreach($this->digits as &$slice) {
foreach($slice[0] as &$sliceName){
foreach($slice[1] as $sliceProduct) {
foreach($sliceProduct as $pKey=>$pVal) {
foreach($sliceName as $nKey=>$nVal) {
$tmp[$h] = ($this->gcd($pVal,$nVal) != 1) ? ++$cuml:'';
}
}
$tmp[$h] = $sliceName['m']*$cuml*1.5;
$h++;
$cuml=0;
}$h=0;
$sliceName['f'] = $tmp;
$tmp='';
}
}
foreach($this->digits as &$u){unset($u[1]);}
}
}
$parser = new CSVParser("file.csv");
$parser->parse(); //print_r($parser->output);
$parser->algorithm(); print_r($parser->digits);
每个请求的示例CSV
Jeff Goes, Mika Enrar;Triple Threat, Dogs on Bikes
Sonny Ray, Lars McGarvitch, Jason McKinley;Kasabian, Lords of Acid, Hard-Fi
输出
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[v] => 3
[c] => 3
[t] => 8
[m] => 3
[f] => Array
(
[0] => 40.5
[1] => 4.5 // Remainder.. So 'Jeff Goes' => 'Dogs on Bikes'
)
)
[1] => Array
(
[v] => 3
[c] => 4
[t] => 9
[m] => 4.5
[f] => Array
(
[0] => 67.5 // High Score! So 'Mika Enrar' => 'Triple Threat'
[1] => 13.5
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[v] => 4
[c] => 2
[t] => 8
[m] => 2
[f] => Array
(
[0] => 24
[1] => 12
[2] => 24 // Next Highest 'Sonny Ray' => 'Hard-Fi'
)
)
[1] => Array
(
[v] => 3
[c] => 8
[t] => 14
[m] => 8
[f] => Array
(
[0] => 84 // High Score! (This is really a tie, but 'm' has the highest secondary value so...)
[1] => 60 // 'Lars McGarvitch => 'Kasabian'
[2] => 84
)
)
[2] => Array
(
[v] => 5
[c] => 5
[t] => 13
[m] => 7.5
[f] => Array
(
[0] => 0
[1] => 0 // The only one left 'Jason McKinley' => 'Lords of Acid'
[2] => 11.25
)
)
)
)
)
它的作用
到目前为止,这个类所做的是将csv拆分为一个数组,之前拆分内容;并进入两个子阵列后。计算两者的辅音和元音,找出每个C V或混合字母对的两个子部分之间是否存在最大公分母,并创建一个值以将带分配给产品。
真正需要做什么
生成的最高值应与创建该高值的波段相关联。所以我真正想做的是将一个名字与一个乐队联系起来,这取决于它最终产生的得分有多高。我大约一半=(
正如大家们所看到的,这段代码实际上是一团糟。我真正想要的是根据我正在生成的数字为乐队指定名称。
答案 0 :(得分:4)
我必须同意其他所有人的意见......但我想补充一下:
而是更简单地搜索如何遍历 $this->digits
,您应该强烈考虑重新考虑数据的结构 $this->digits
强>
此外,将所有内容整合到一个数组中并不总是有意义的。但是当它发生时,可以考虑结构,使其直观,并且可以轻松遍历。
如果没有关于这是做什么的更多信息,我们无法建议如何重组您的数据/类。一个开始就是给我们一个样本$this->digits
数组的样子。此外,有关您的问题的一些更多信息将是好的(如何使用此方法)。
答案 1 :(得分:0)
如果它有效,你为什么要改变它?性能?重构?生意改变了?要求改变了?清洁代码撒玛利亚人?童子军规则?
当我遇到“意大利面条代码”时,除非我绝对必须改变它,否则我不管它。也就是说,我会写一些单元测试来验证“意大利面条代码”的输出,以便我知道我没有破坏任何东西或者让事情变得更糟。