This works but is uglier than hell, basically it's iterating through two separate portions of a sub array, seeing if there's a greatest common denominator besides 1 in the values of both sub arrays, and if there is, multiplying the base value by 1.5
Sorry for the sloppy code ahead of time.
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);
Sample CSV per request
Jeff Goes, Mika Enrar;Triple Threat, Dogs on Bikes
Sonny Ray, Lars McGarvitch, Jason McKinley;Kasabian, Lords of Acid, Hard-Fi
The Output
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
)
)
)
)
)
What it does
What this class does so far is split the csv one array, split content prior to ; and after into two sub arrays. Count the consonants and vowels of both, find if there is a greatest common denominator between the two subsections for each C V or mixed letter pair, and create a value to assign a band to a product.
What really needs to do though
The highest value generated should be associated with the band that created that high value. So what I am trying to really do is associate a name to a band depending on how high of a score it ultimately generates. I'm about half way through =(
As you guys can see, this code is a mess, literally. All I really want is to assign a name to a band based on the numbers I'm generating.
I have to agree with everyone else here... but I'd like to add:
Instead searching for how to traverse $this->digits more simply, you should strongly consider rethinking the structure of the data in $this->digits.
Furthermore, lumping everything into a single array doesn't always make sense. But when it does, the structure can be thought out so that it is intuitive and can be traversed easily.
Without more information about what this is doing, there is no way for us to suggest how to restructure your data / class. A start would be giving us what a sample $this->digits array looks like. Also, some more information about your problem would be good (like how this method is used).
If it works why are you changing it? Performance? Refactor? Business changed? Requirements changed? Clean code Samaritan? Boy Scout rule?
When I come across "spaghetti code" I leave it alone unless I absolutely must change it. That said, I would write a couple of unit tests verifying the output of the "spaghetti code" so that I know that I did not break anything or make things worse.
Related
I have a string
Label 1|80|default
Label 2|100|default
---
Frontend|80|red
Backend|20|red
---
Another Item 1|20|blue
Another Item 2|20|blue
-
And another Item|20|blue
which should be converted to the following array
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[label] => Label 1
[value] => 80
[color] => default
)
[1] => Array
(
[label] => Label 2
[value] => 100
[color] => default
)
)
)
[1] => Array
(
[items] => Array
(
[0] => Array
(
[label] => Frontend
[value] => 80
[color] => red
)
[1] => Array
(
[label] => Backend
[value] => 20
[color] => red
)
)
)
[2] => Array
(
[items] => Array
(
[0] => Array
(
[label] => And another Item
[value] => 20
[color] => blue
)
)
)
)
to be converted into json:
[{"items":[{"label":"Label 1","value":"80","color":"default"},{"label":"Label 2","value":"100","color":"default"}]},{"items":[{"label":"Frontend","value":"80","color":"red"},{"label":"Backend","value":"20","color":"red"}]},{"items":[{"label":"And another Item","value":"20","color":"blue"}]}]
Currently, my way of doing that string splitting to multiple arrays seems a bit complicated to me. At least, it is hard to read.
protected function convertPartsToObject($parts): array
{
$return = [];
$arr1 = explode("---", trim($parts));
foreach ($arr1 as $arr1Item) {
$arr2 = explode("-", trim($arr1Item));
foreach ($arr2 as $arr2Item) {
$arr3 = explode("\n", trim($arr2Item));
$group = [];
foreach ($arr3 as $arr3Item) {
$arr4 = explode("|", trim($arr3Item));
$item = [
'label' => $arr4[0],
'value' => $arr4[1],
'color' => $arr4[2]
];
$group['items'][] = $item;
}
}
$return[] = $group;
}
return $return;
}
Question:
Is there a better / more readable way to convert strings by multiple delimiters to an associative array? Maybe with a nested regex?
Background, just as an info:
This is part of a content management system, where I need some data as json in frontend (for react). And I don't want the editors to learn the json syntax...
This is an alternative to your current code. Instead of the nested explode() it just splits the input into lines and then processes the lines. For --- it just adds the current data into the result, - removes the current stored data(not sure if it should), else it extracts the data (I've used the ability to explode to an associative array as a shorthand, depends if you like it or not you can use your current method)...
function convertPartsToObject ( $parts ) : array {
$return = [];
$buffer = [];
foreach ( explode( PHP_EOL, $parts ) as $line ) {
if ( trim($line) == "---" ) {
$return[]['items'] = $buffer;
$buffer = [];
}
elseif ( trim($line) == "-" ) {
$buffer = [];
}
else {
[$arr4['label'], $arr4['value'], $arr4['color']] = explode("|", trim($line));
$buffer[] = $arr4;
}
}
// Add last items
$return[]['items'] = $buffer;
return $return;
}
Rather than maintaining temporary arrays to be pushed into a parent array when they are "completed" and after the loop is finished, this is an excellent case for implementing a reference variable. Think of the reference variable as a magical soccer goal -- once declared, you can close your eyes, kick your soccer ball and no matter where the ball travels, it will swish into the back of the net as desired. When you want to have a new soccer goal, just destroy the old one (unset()) and create a new one with the same technique.
This technique reduces the code length and is easily read by humans -- so long as you understand how reference variables work.
I should include a caveat, that if your input array is empty, you'll probably want an early return in your method so that the reference variable never pushes any entries into your result array.
Code: (Demo)
function stringToGroupedArray(string $string): array
{
$result = [];
$result[]['items'] = &$items;
foreach (explode(PHP_EOL, $string) as $line) {
if ($line[0] === '-') { // or however you want to identify the separator
unset($items);
$result[]['items'] = &$items;
} else {
$items[] = array_combine(['label', 'value', 'color'], explode('|', $line, 3));
}
}
return $result;
}
I am using PHP 7.3.5 and I have the following set of array values:
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
Basically I want for each of these values the number and the "type", so if it is a percentage then I would like to get -4.2 and percentage.
I tried to create a minimum example (however the below code is no real good example ;( ), but I am stuck at the data structure level as some array keys have two inputs, such as '0.74|2.6':
<?php
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
$resArr = array();
$structureArr = array(
'value1' => "",
'number1' => "",
'value2' => "",
'number2' => ""
);
foreach ($valueArr as $key => $v) {
if (1 === preg_match('/%/', $valueArr[$key])) {
preg_match('!\d+\.*\d*!', $valueArr[$key], $structureArr['number1']);
$structureArr['value1'] = 'percentage';
}
/*
if (1 === preg_match('|', $valueArr[$key])) {
$str = explode("|", $valueArr[$key]);
$value1 = 'number';
$number1 = $str[0];
$value2 = 'number';
$number2 = $str[1];
}
if (1 === preg_match('', $valueArr[$key])) {
}
*/
array_push($resArr, $structureArr);
}
print_r($resArr);
/*
Wanted Result
Array
(
[0] => Array
(
[0] => -4.2
[1] => 'percentage'
)
[1] => Array
(
[0] => 51.0
[1] => 'number'
)
[2] => Array
(
[0] => 90000
[1] => number
)
[3] => Array
(
[0] => 0.5
[1] => percentage
)
[4] => Array
(
[0] => 0.74
[1] => number
[2] => 2.6
[3] => number
)
...
*/
I would highly appreciate your input on how to structure this array input.
Appreciate your replies!
If you join the array on a space and replace pipes | with a space, then you have a list of numbers and their symbol (if any) separated by a space. Then just match your numbers and whatever symbol comes after it. Then you just match the number index with the symbol index. I used an array to map the symbol to the word and number if none:
$string = str_replace('|', ' ', implode(' ', $valueArr));
preg_match_all('/([\d.-]+)([^\s]*)/', $string, $matches);
$types = ['%'=>'percent','K'=>'thousand','M'=>'million','B'=>'billion','T'=>'trillion'];
foreach($matches[1] as $k => $v) {
$t = $types[$matches[2][$k]] ?? 'number';
$result[] = [$v, $t];
}
This yields an array like this, with each number that was joined by a pipe with it's own element:
Array
(
[0] => Array
(
[0] => -4.2
[1] => percent
)
[1] => Array
(
[0] => 51.0
[1] => number
)
[2] => Array
(
[0] => 90
[1] => thousand
)
///etc...
If you need a floating point number then just change:
$result[] = [(float)$v, $t];
This expands on my comment. Not sure if it's the most optimal solution or not.
Rough outline...
Create array mapping suffix to multiplier. Loop through source array. explode on |. Loop through result. If last character is %, strip it, value=value and type=percentage, else, strip last char, use it as array index (if it is an available index), value=value*multiplier and type=number.
$resArr = array();
$multipliers = array("K" => 1000, "M" => 1000000, "B" => 1000000000, "T" => 1000000000000);
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
foreach($valueArr as $index => $value)
{
$parts = explode("|", $value);
$resArr[$index] = array();
foreach($parts as $part)
{
$lastChar = substr($part, -1);
if($lastChar == "%")
{
$resArr[$index][] = substr($part, 0, -1);
$resArr[$index][] = "percentage";
}
else if(in_array($lastChar, array_keys($multipliers)))
{
$multiple = $multipliers[$lastChar];
$resArr[$index][] = (substr($part, 0, -1))*$multiple;
$resArr[$index][] = "number";
}
else
{
$resArr[$index][] = $part;
$resArr[$index][] = "number";
}
}
}
var_dump($resArr);
DEMO
I've got a multidimensional array.
I need a way to tally up the total value when both the 1st and second strings in the array occur multiple times.
So for instance :
Gold Metallic = 22
Black Toscano = 26
etc...
Any ideas?
[0] => Array
(
[0] => Array
(
[0] => Black
[1] => Toscano
[2] => 14
)
[1] => Array
(
[0] => Gold
[1] => Metallic
[2] => 10
)
)
[1] => Array
(
[0] => Array
(
[0] => Gold
[1] => Metallic
[2] => 12
)
[1] => Array
(
[0] => Black
[1] => Toscano
[2] => 12
)
)
This just solves the problem for your data structure so you have to make sure that, in practice, every two items you will get a number. Hope you can learn something from this :)
$products = array(
array(
array("Black", "Toscano", 14),
array("Gold", "Metallic", 10)
),
array(
array("Black", "Toscano", 12),
array("Gold", "Metallic", 12)
),
);
$accumulated = array();
$key = "";
$callback = function($item, $index) use(&$key, &$accumulated) {
if($index != 2) {
$key .= $item;
} else {
if(!array_key_exists($key, $accumulated)) {
$accumulated[$key] = 0;
}
$accumulated[$key] += $item;
$key = "";
}
};
array_walk_recursive($products, $callback);
var_dump($accumulated);
Should be a simple case of looping over the data and storing an array of sums. This is one possibility using a hash with keys as the pairs concatenated with a separator sentinel value.
$separator = "||"; //take care to choose something that doesn't pop up in your data here
//$data = example data;
$pairs = array();
foreach ($data as $val) {
foreach ($val as $pair) {
$str = $pair[0] . $separator . $pair[1];
if (array_key_exists($str, $pairs))
$pairs[$str] += $pair[2];
else
$pairs[$str] = $pair[2];
}
}
print_r($pairs);
output:
["Black||Toscano"] => 26,
["Gold||Metallic"] => 22
The data can be easily retrieved at this point
foreach ($pairs as $str => $sum) {
$str = explode($separator, $str);
echo $str[0] . ", " . $str[1] . ": " . $sum;
}
Im making a tree to store words and an associated number array in php. I need it to look something like this:
Words: apple, ant
[a] => Array
(
[p] => Array
(
[p] => Array
(
[l] => Array
(
[e] => Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 1
[3] => 2
[4] => 3
[5] => 4
)
)
)
)
)
[n] => Array
(
[t] => Array
(
[0] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 0
[4] => 0
[5] => 4
)
)
)
)
Of course apple and ant need to share the same [a] index. Im close, but I cant figure out how to properly keep track of the tree index so 'apple' gets into the tree fine but 'ant' is inserted as 'nt'. Heres my code at the moment:
private function insertWordsIntoTree()
{
foreach ($this->words as $word)
{
$characters = preg_replace('/[0-9]+/', '', $words);
$points = $this->getPoints($word);
$this->tree = $this->buildTree($characters, $points, $this->tree);
}
print_r($this->tree);
}
private function buildTree($characters, array $points, array $tree)
{
for ($i = 0; $i < strlen($characters); $i++)
{
$character = $characters[$i];
$remaining_characters = substr($characters, $i + 1);
if (strlen($characters) === 1)
{
$child = [];
$child[$character] = [$points];
return $child;
}
elseif (!isset($tree[$character]))
{
$tree[$character] = $this->buildTree($remaining_characters, $points, []);;
break;
}
else
{
$this->buildTree($remaining_characters, $points, $tree[$character]);
}
}
return $tree;
}
Im pretty sure the problem is at the else statement...I dont think Im keeping track of the current tree index properly. Any help would be much appreciated.
Here's a simple approach that passes the recursion off to php:
$tree = array();
foreach($words as $word) {
$characters = array_reverse(str_split($word));
$temp = array();
foreach($characters as $index => $character) {
if($index == 0) {
$temp[] = getPoints($word);
}
$temp = array(
$character => $temp
);
}
$tree = array_merge_recursive($tree, $temp);
}
I have an arbitrary number of nested arrays in php. For example:
Array
(
[0] => Array
(
[0] => 36
[0] => 2
[0] => 9
)
[1] => Array
(
[0] => 95
[1] => 21
[2] => 102
[3] => 38
)
[2] => Array
(
[0] => 3
[1] => 5
)
)
I want to find the most efficient way to combine all possible combinations of each of these nested arrays. I'd like to end up with something that looks like this...
Array
(
[0] => "36,95,3"
[1] => "36,95,5"
[2] => "36,21,3"
[3] => "36,21,5"
etc...
)
The order the results are combined in is not important. That is to say, there is no difference between "3,95,36", "36,95,3", and "95,36,3". I would like to omit these redundant combinations.
Any suggestions on how to go about this would be much appreciated.
Thanks in advance,
<?php
$a = array("01", "02");
$b = array("white", "green");
$c = array("one", "two", "three");
$aG = array($a, $b, $c);
$codes = array();
$pos = 0;
generateCodes($aG);
function generateCodes($arr) {
global $codes, $pos;
if(count($arr)) {
for($i=0; $i<count($arr[0]); $i++) {
$tmp = $arr;
$codes[$pos] = $arr[0][$i];
$tarr = array_shift($tmp);
$pos++;
generateCodes($tmp);
}
} else {
echo join(", ", $codes)."<br/>";
}
$pos--;
}
?>
General permutation solution for any number of groups (in Groovy to keep it simple).
list = [["A", "B", "W"], ["C", "D"], ["X", "Y"]]
r = []
void p(List<List<String>> l, int n) {
for(t in l[n]) {
r[n] = t
if (n < l.size() - 1) {
p(l, n + 1)
} else {
println(r)
}
}
}
p(list, 0);