function assignNames(array $source, array $names) { $result = array(); foreach ($source as $key => $value) { if (is_array($value)) { $result[$names[$key]] = assignNames($source[$key], $names); } else { $result[$names[$key]] = $names[$value]; } } return $result; }
private function load() { $last = array(-1 => -1); $beforeType = 0; $data = array(); $keys = array(); $file = fopen($this->fileName, 'r'); $key = 0; while (!feof($file)) { $value = fgets($file); $type = substr($value, 0, 1) * 1; $data = substr($value, 1, strlen($value) - 2); $this->types[] = $type; $this->data[] = $data; $this->parents[$key] = $last[$type - 1]; $this->linesBytes[] = fTell($file); if ($type - 1 > $beforeType) { $line = $key + 1; $excMsg = "Incorrect declaration of type in '" . $this->database . "' line " . $line; throw new StormDBException($excMsg, 0); } if ($type == 0) { $this->collections[] = $data; } $last[$type] = $key; $beforeType = $type; $key++; } $parentsBuild = array('BASE' => array()); for ($depth = 0; $depth < 10; $depth++) { foreach ($this->types as $key => $type) { if ($type == $depth) { if ($this->parents[$key] >= 0) { if (!isset($parentsBuild[$this->parents[$key]])) { $parentsBuild[$this->parents[$key]] = array(); } $parentsBuild[$this->parents[$key]][$key] = $key; } else { $parentsBuild['BASE'][$key] = $key; } } } } $this->build = assignNames(buildTree($parentsBuild, 'BASE'), $this->data); }
<?php require '../../StormDB/StormDBUtils.php'; $source = array(1 => array(2 => array(3 => 3), 4 => array(5 => 5)), 6 => array(7 => 7)); $names = array(1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six', 7 => 'Seven'); $result = assignNames($source, $names); var_dump($result);