Created
March 6, 2014 20:03
-
-
Save RobertZenz/9398233 to your computer and use it in GitHub Desktop.
http://codereview.stackexchange.com/questions/43635/how-can-i-optimize-this-database-to-array-structure-code - Speed Test
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
<?php | |
class Item { | |
public $id; | |
public $type; | |
public $value; | |
public $label; | |
public $optgroup; | |
function __construct($id, $type, $value, $label, $optgroup) { | |
$this->id = $id; | |
$this->type = $type; | |
$this->value = $value; | |
$this->label = $label; | |
$this->optgroup = $optgroup; | |
} | |
} | |
class Container { | |
private $items; | |
function __construct($items) { | |
$this->items = $items; | |
} | |
public function result() { | |
return $this->items; | |
} | |
} | |
function _format($STH) | |
{ | |
$data = []; | |
foreach ($STH->result() as $row) | |
{ | |
if ( ! $row->optgroup) | |
$data[ $row->type ][ $row->value ] = $row->label; | |
else | |
$data[ $row->type ][ $row->optgroup ][ $row->value ] = $row->label; | |
} | |
// selects with a single optgroup can have that optgroup removed | |
foreach ($data as $menutype => $optorkey) | |
{ | |
if (is_array($optorkey) && count($optorkey) == 1) | |
$data[$menutype] = current($optorkey); | |
} | |
return $data; | |
} | |
$container = new Container( | |
array( | |
new Item(1, "car", "ix5", "Ford Taurus", "Ford"), | |
new Item(2, "car", "ix6", "Ford Focus", "Ford"), | |
new Item(3, "car", "ix9", "Cobalt", "Chevy"), | |
new Item(4, "planet", "ix8", "Earth", "Default") | |
) | |
); | |
for ($count = 0; $count < 100000; $count++) { | |
_format($container); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment