Ticketa
Registered
Привет, някой може ли да ми помогне възможно най-кратко да обработя следния масив и да изглежда с възможно най-малко код:
(метода/функцията getTree на този етап не ми се иска да пипам, а единствено изходния return)
Тук в контролера ги извеждам със следната опция:
print_r(\App\Models\Post::getTree());
ЕДИТ: Включих хахахаха
(метода/функцията getTree на този етап не ми се иска да пипам, а единствено изходния return)
Parent
-- Child
-- Child
Parent
Parent
Parent
PHP:
/**
*
* Select All Categories
*
* Using $child
*
*/
public static function getTree($child = null)
{
$db = static::getDB();
if ($child)
$where = '`posts_category`.`category_parent_id` = ' . $child;
else
$where = '`posts_category`.`category_parent_id` = 0 ';
$sth = $db->prepare("SELECT *
FROM
`posts_category`
LEFT JOIN `posts_category_translate` ON
`posts_category`.`category_id` = `posts_category_translate`.`category_id`
WHERE " . $where . " ORDER BY `posts_category`.`category_id` DESC;
");
$sth->execute();
if ($child) {
$result = $sth->fetchAll();
$return = $result;
} else { //get parents
while ($result = $sth->fetch())
{
//one by one
$return[] = array( 'parent' => $result, //parent info
'childs' => static::getTree($result['category_id']) //childs
);
}
}
return $return;
}
Тук в контролера ги извеждам със следната опция:
print_r(\App\Models\Post::getTree());
Код:
Array
(
[0] => Array
(
[parent] => Array
(
[category_id] => 7
[0] => 7
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 7
[3] => 7
[category_lang] => en
[4] => en
[5] => 7
[category_slug] => parent-10
[6] => parent-10
[category_name] => Parent 10
[7] => Parent 10
[category_description] =>
[8] =>
)
[childs] => Array
(
[0] => Array
(
[category_id] => 9
[0] => 9
[category_parent_id] => 7
[1] => 7
[category_icon] =>
[2] =>
[category_translate_id] => 9
[3] => 9
[category_lang] => en
[4] => en
[5] => 9
[category_slug] => parent-9
[6] => parent-9
[category_name] => Parent 9
[7] => Parent 9
[category_description] =>
[8] =>
)
[1] => Array
(
[category_id] => 8
[0] => 8
[category_parent_id] => 7
[1] => 7
[category_icon] =>
[2] =>
[category_translate_id] => 8
[3] => 8
[category_lang] => en
[4] => en
[5] => 8
[category_slug] => parent-8
[6] => parent-8
[category_name] => parent 8
[7] => Parent 8
[category_description] =>
[8] =>
)
)
)
[1] => Array
(
[parent] => Array
(
[category_id] => 6
[0] => 6
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 6
[3] => 6
[category_lang] => en
[4] => en
[5] => 6
[category_slug] => parent-6
[6] => parent-6
[category_name] => Parent 6
[7] => Parent 6
[category_description] =>
[8] =>
)
[childs] => Array
(
)
)
[2] => Array
(
[parent] => Array
(
[category_id] => 5
[0] => 5
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 5
[3] => 5
[category_lang] => en
[4] => en
[5] => 5
[category_slug] => parent-5
[6] => Parent 5
[category_name] => parent-5
[7] => Parent 5
[category_description] =>
[8] =>
)
[childs] => Array
(
)
)
[3] => Array
(
[parent] => Array
(
[category_id] => 4
[0] => 4
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 4
[3] => 4
[category_lang] => en
[4] => en
[5] => 4
[category_slug] => parent-4
[6] => parent-4
[category_name] => Parent 4
[7] => Parent 4
[category_description] =>
[8] =>
)
[childs] => Array
(
)
)
[4] => Array
(
[parent] => Array
(
[category_id] => 3
[0] => 3
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 3
[3] => 3
[category_lang] => en
[4] => en
[5] => 3
[category_slug] => parent-3
[6] => Parent 3
[category_name] => parent-3
[7] => Parent 3
[category_description] =>
[8] =>
)
[childs] => Array
(
)
)
[5] => Array
(
[parent] => Array
(
[category_id] => 2
[0] => 2
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 2
[3] => 2
[category_lang] => en
[4] => en
[5] => 2
[category_slug] => parent-2
[6] => parent-2
[category_name] => Parent 2
[7] => Parent 2
[category_description] =>
[8] =>
)
[childs] => Array
(
)
)
[6] => Array
(
[parent] => Array
(
[category_id] => 1
[0] => 1
[category_parent_id] => 0
[1] => 0
[category_icon] =>
[2] =>
[category_translate_id] => 1
[3] => 1
[category_lang] => en
[4] => en
[5] => 1
[category_slug] => others
[6] => others
[category_name] => Others
[7] => Others
[category_description] =>
[8] =>
)
[childs] => Array
(
)
)
)
ЕДИТ: Включих хахахаха
Код:
foreach (\App\Models\Post::getTree() as $category) {
// Print out information about the parent category
echo "Parent Category ID: " . $category['parent']['category_id'] . "\n";
echo "Parent Category Name: " . $category['parent']['category_name'] . "\n";
// Print out information about the child categories
foreach ($category['childs'] as $child) {
echo "Child Category ID: " . $child['category_id'] . "\n";
echo "Child Category Name: " . $child['category_name'] . "\n";
}
}
Последно редактирано: