Как правильно сравнить и вывести данные?

Доброго всем!

Подскажите как правильно сравнить и вывести в массив данные из БД.

Как сравнить поля $category['category_id'] и $category['parent_id'] и если они совпадают то добавить их в массив $child.

Уже есть вот такой массив:

[62] => Array
        (
            [category_id] => 62
            [image] => 
            [parent_id] => 0
            [top] => 1
            [column] => 1
            [sort_order] => 0
            [status] => 1
            [date_added] => 2020-05-23 13:47:51
            [date_modified] => 2020-05-23 14:47:26
            [language_id] => 1
            [name] => Металлопрокат
            [description] => 
            [meta_title] => 
            [meta_h1] => 
            [meta_description] => 
            [meta_keyword] => 
            [store_id] => 0
            [href] => http://test.bbden.ru/index.php?route=product/category&path=62
            [childs] => Array
                (
                    [63] => Array
                        (
                            [category_id] => 63
                            [image] => 
                            [parent_id] => 62
                            [top] => 0
                            [column] => 1
                            [sort_order] => 0
                            [status] => 1
                            [date_added] => 2020-05-23 13:48:32
                            [date_modified] => 2020-05-23 13:48:32
                            [language_id] => 1
                            [name] => Труба металическая
                            [description] => 
                            [meta_title] => 
                            [meta_h1] => 
                            [meta_description] => 
                            [meta_keyword] => 
                            [store_id] => 0
                            [href] => http://test.bbden.ru/index.php?route=product/category&path=63
                            [childs] => Array
                                (
                                    [64] => Array
                                        (
                                            [category_id] => 64
                                            [image] => 
                                            [parent_id] => 63
                                            [top] => 0
                                            [column] => 1
                                            [sort_order] => 0
                                            [status] => 1
                                            [date_added] => 2020-05-23 13:48:58
                                            [date_modified] => 2020-05-23 13:49:12
                                            [language_id] => 1
                                            [name] => Труба электросварная
                                            [description] => 
                                            [meta_title] => 
                                            [meta_h1] => 
                                            [meta_description] => 
                                            [meta_keyword] => 
                                            [store_id] => 0
                                            [href] => http://test.bbden.ru/index.php?route=product/category&path=64
                                        )

                                )

                        )

                )

        )


Получаю его так:

<?php
class ControllerExtensionModuleCategorywallhome extends Controller {
	
	protected function debug($data){
		echo '<pre>'. print_r($data, true) .'</pre>';
	}

	public function index() {
		$this->load->language('extension/module/categorywallhome');

		$data['heading_title'] = $this->language->get('heading_title');

		if (isset($this->request->get['path'])) {
			$parts = explode('_', (string)$this->request->get['path']);
		} else {
			$parts = array();
		}

		if (!empty($parts)) {
			$data['active'] = end($parts);
		}else{
			$data['active'] = 0;
		}
		

		$this->load->model('catalog/categorywallhome');

		$data['categories'] = array();

		$categories = $this->model_catalog_categorywallhome->getwallCategories();
		
		foreach($categories as $id => $category){
			$categories[$id]['href'] =  $this->url->link('product/category', 'path=' . $category['category_id']);
		}
		$data['categories_tree'] = $categories = $this->model_catalog_categorywallhome->getMapTree($categories);
		$this->debug($categories);


		return $this->load->view('extension/module/categorywallhome', $data);
	}
}


Сам запрос в БД :

<?php
class ModelCatalogCategorywallhome extends Model {
	public function getwallCategories() {
		$query = $this->db->query(
			"SELECT * FROM " . DB_PREFIX . "category c 
			LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) 
			LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) 
			WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");

		$cats = [];
		foreach($query->rows as $row){
			$cats[$row['category_id']] = $row;
		}
		return $cats;
	}
	public function getMapTree($dataset){
		$tree = array();

		foreach ($dataset as $id => &$node) {
			if (!$node['parent_id']) {
				$tree[$id] = &$node;
			}else{
				$dataset[$node['parent_id']]['childs'][$id] = &$node;
			}
		}
		return $tree;
	}
}


На выходе нужно получить такой массив только с потомками в children:
Array
(
    [0] => Array
        (
            [category_id] => 62
            [name] => Металлопрокат
            [children] => Array
                (
                )

            [href] => http://test.bbden.ru/index.php?route=product/category&path=62
        )

    [1] => Array
        (
            [category_id] => 59
            [name] => Пиломатериалы
            [children] => Array
                (
                )

            [href] => http://test.bbden.ru/index.php?route=product/category&path=59
        )
  • Вопрос задан
  • 80 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы