Last active
August 29, 2015 14:18
-
-
Save ttskch/33f2c37d946d380fb1f0 to your computer and use it in GitHub Desktop.
Nagoya.php vol.9 プログラミング問題解答例
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 | |
$data = [ | |
[ | |
'id' => 1, | |
'parent_id' => 0, | |
'value' => '親1', | |
], | |
[ | |
'id' => 2, | |
'parent_id' => 0, | |
'value' => '親2', | |
], | |
[ | |
'id' => 3, | |
'parent_id' => 1, | |
'value' => '子1-1', | |
], | |
[ | |
'id' => 4, | |
'parent_id' => 1, | |
'value' => '子1-2', | |
], | |
[ | |
'id' => 5, | |
'parent_id' => 2, | |
'value' => '子2-1', | |
], | |
]; | |
function render() | |
{ | |
global $data; | |
// 親だけを抽出. | |
$parents = array_filter($data, function ($node) { | |
return $node['parent_id'] === 0; | |
}); | |
echo '<ul>'; | |
// 親のループ. | |
foreach ($parents as $parent) { | |
echo "<li>{$parent['value']}"; | |
// 自分の子だけを抽出. | |
$children = array_filter($data, function ($node) use ($parent) { | |
return $node['parent_id'] === $parent['id']; | |
}); | |
// 子のループ. | |
if ($children) { | |
echo '<ul>'; | |
foreach ($children as $child) { | |
echo "<li>{$child['value']}</li>"; | |
} | |
echo '</ul>'; | |
} | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
} | |
render(); |
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 | |
return [ | |
[ | |
'id' => 1, | |
'parent_id' => 0, | |
'value' => '親1', | |
], | |
[ | |
'id' => 2, | |
'parent_id' => 0, | |
'value' => '親2', | |
], | |
[ | |
'id' => 3, | |
'parent_id' => 1, | |
'value' => '子1-1', | |
], | |
[ | |
'id' => 4, | |
'parent_id' => 1, | |
'value' => '子1-2', | |
], | |
[ | |
'id' => 5, | |
'parent_id' => 2, | |
'value' => '子2-1', | |
], | |
[ | |
'id' => 6, | |
'parent_id' => 4, | |
'value' => '孫1-2-1', | |
], | |
[ | |
'id' => 7, | |
'parent_id' => 3, | |
'value' => '孫1-1-1', | |
], | |
[ | |
'id' => 8, | |
'parent_id' => 7, | |
'value' => 'ひ孫1-1-1-1', | |
], | |
[ | |
'id' => 9, | |
'parent_id' => 5, | |
'value' => '孫2-1-1', | |
], | |
[ | |
'id' => 10, | |
'parent_id' => 5, | |
'value' => '孫2-1-2', | |
], | |
[ | |
'id' => 11, | |
'parent_id' => 2, | |
'value' => '子2-2', | |
], | |
[ | |
'id' => 12, | |
'parent_id' => 4, | |
'value' => '孫1-2-2', | |
], | |
[ | |
'id' => 13, | |
'parent_id' => 9, | |
'value' => 'ひ孫2-1-1-1', | |
], | |
[ | |
'id' => 14, | |
'parent_id' => 5, | |
'value' => '孫2-1-3', | |
], | |
[ | |
'id' => 15, | |
'parent_id' => 2, | |
'value' => '子2-3', | |
], | |
]; |
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 | |
$data = require __DIR__ . '/data.php'; | |
function renderSubtree($parentId) | |
{ | |
global $data; | |
// 指定された親だけを抽出. | |
$parents = array_filter($data, function ($node) use ($parentId) { | |
return $node['parent_id'] === $parentId; | |
}); | |
// 見つからなければ何もしない. | |
if (!$parents) { | |
return; | |
} | |
echo '<ul>'; | |
// 親のループ. | |
foreach ($parents as $parent) { | |
echo "<li>{$parent['value']}"; | |
// 子以下を出力. | |
renderSubtree($parent['id']); | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
} | |
renderSubtree(0); |
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 | |
$data = require __DIR__ . '/data.php'; | |
class Renderer | |
{ | |
const CLI_INDENT = ' '; | |
private $data; | |
private $indents; | |
private $isCli; | |
public function __construct(array $data) | |
{ | |
$this->data = $data; | |
$this->indents = -1; | |
$this->isCli = php_sapi_name() === 'cli'; | |
} | |
public function renderSubtree($parentId) | |
{ | |
// 指定された親だけを抽出. | |
$parents = array_filter($this->data, function ($node) use ($parentId) { | |
return $node['parent_id'] === $parentId; | |
}); | |
// 見つからなければ何もしない. | |
if (!$parents) { | |
return; | |
} | |
$this->openTree(); | |
// 親のループ. | |
foreach ($parents as $parent) { | |
$this->openNode($parent['value']); | |
// 子以下を出力. | |
$this->renderSubtree($parent['id']); | |
$this->closeNode(); | |
} | |
$this->closeTree(); | |
} | |
private function openTree() | |
{ | |
if ($this->isCli) { | |
$this->indents++; | |
} else { | |
echo '<ul>'; | |
} | |
} | |
private function closeTree() | |
{ | |
if ($this->isCli) { | |
$this->indents--; | |
} else { | |
echo '</ul>'; | |
} | |
} | |
private function openNode($value) | |
{ | |
if ($this->isCli) { | |
echo str_repeat(self::CLI_INDENT, $this->indents); | |
echo "* {$value}\n"; | |
} else { | |
echo "<li>{$value}"; | |
} | |
} | |
private function closeNode() | |
{ | |
if (!$this->isCli) { | |
echo '</li>'; | |
} | |
} | |
} | |
$renderer = new Renderer($data); | |
$renderer->renderSubtree(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment