Last active
May 25, 2016 10:35
-
-
Save iolloyd/3611855 to your computer and use it in GitHub Desktop.
Super simple strategy to pivot table like data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pivot { | |
protected $pivotedData; | |
public function __construct($data){ | |
$this->pivotedData = $this->_pivot($data); | |
} | |
protected function pivot($data){ | |
$pivoted = []; | |
foreach ($data as $key => $values) { | |
foreach ($values as $v) { | |
if (!isset($pivoted[$v])) { | |
$pivoted[$v] = array(); | |
} | |
$pivoted[$][] = $key; | |
} | |
} | |
return $pivoted; | |
} | |
public function getPivotedData() { | |
return $this->pivotedData; | |
} | |
} | |
// a simple test ... | |
$data = [ | |
'x' => ['a', 'b', 'c'], | |
'y' => ['a', 'c', 'd'], | |
]; | |
$x = new Pivot($data); | |
print_r($x->getPivotedData()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment