Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Last active May 25, 2016 10:35
Show Gist options
  • Save iolloyd/3611855 to your computer and use it in GitHub Desktop.
Save iolloyd/3611855 to your computer and use it in GitHub Desktop.
Super simple strategy to pivot table like data
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