Как я могу объединить реляционные данные из 3 или более запросов в многомерный массив? - PullRequest
0 голосов
/ 08 июля 2019

Мне нужно найти способ собрать реляционные данные из нескольких источников данных.Вот как выглядит моя структура данных, исходящая из источника данных.Я хочу быть в состоянии вывести его в многомерный массив.Любая помощь будет принята с благодарностью.Заранее спасибо.

Array
(
    [0] => Array
        (
            [object_name] => statements
            [id] => statement_id
            [link_object_id] => check_id
            [link_object_name] => checks
            [level] => 2
            [rows] => Array
                (
                    [0] => Array
                        (
                            [check_id] => 1
                            [statement_id] => 1
                            [date] => 2018-01-01
                        )

                    [1] => Array
                        (
                            [check_id] => 2
                            [statement_id] => 1
                            [date] => 2018-01-01
                        )

                    [2] => Array
                        (
                            [check_id] => 3
                            [statement_id] => 2
                            [date] => 2018-01-02
                        )

                    [3] => Array
                        (
                            [check_id] => 4
                            [statement_id] => 1
                            [date] => 2018-01-01
                        )

                    [4] => Array
                        (
                            [check_id] => 5
                            [statement_id] => 2
                            [date] => 2018-01-02
                        )
                )
        )
)


Array
(
    [0] => Array
        (
            [object_name] => checks
            [id] => check_id
            [link_object_id] => employee_id
            [link_object_name] => employees
            [level] => 1
            [rows] => Array
                (
                    [0] => Array
                        (
                            [check_id] => 1
                            [employee_id] => 1
                            [amount] => 100.00
                        )

                    [1] => Array
                        (
                            [check_id] => 2
                            [employee_id] => 1
                            [amount] => 200.00
                        )

                    [2] => Array
                        (
                            [check_id] => 3
                            [employee_id] => 2
                            [amount] => 10.00
                        )

                    [3] => Array
                        (
                            [check_id] => 4
                            [employee_id] => 1
                            [amount] => 300.00
                        )

                    [4] => Array
                        (
                            [check_id] => 5
                            [employee_id] => 2
                            [amount] => 30.00
                        )
                )
        )
)

Array
(
    [0] => Array
        (
            [object_name] => vacation
            [id] => vacation_id
            [link_object_id] => employee_id
            [link_object_name] => employees
            [level] => 1
            [rows] => Array
                (
                    [0] => Array
                        (
                            [vacation_id] => 1
                            [employee_id] => 1
                            [date] => 2016-01-01
                        )

                    [1] => Array
                        (
                            [vacation_id] => 2
                            [employee_id] => 2
                            [date] => 2016-01-01
                        )
                )
        )
)

Array
(
    [0] => Array
        (
            [object_name] => employees
            [id] => employee_id
            [link_object_id] => 
            [link_object_name] => 
            [level] => 0
            [rows] => Array
                (
                    [0] => Array
                        (
                            [employee_id] => 1
                            [name] => John Doe
                        )

                    [1] => Array
                        (
                            [employee_id] => 2
                            [name] => Bob Smith
                        )
                )
        )
)

Мне бы хотелось, чтобы мой вывод выглядел так, как только все объединено.

Array
(
    [0] => Array
        (
            [employee_id] => 1
            [name] => John Doe
            [checks] => Array
                (
                    [0] => Array
                        (
                            [check_id] => 1
                            [employee_id] => 1
                            [amount] => 100.00
                            [statements] => Array
                                (
                                    [0] => Array
                                        (
                                            [check_id] => 1
                                            [statement_id] => 1
                                            [date] => 2018-01-01
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [check_id] => 2
                            [employee_id] => 1
                            [amount] => 200.00
                            [statements] => Array
                                (
                                    [0] => Array
                                        (
                                            [check_id] => 2
                                            [statement_id] => 2
                                            [date] => 2018-01-01
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [check_id] => 4
                            [employee_id] => 1
                            [amount] => 300.00
                            [statements] => Array
                                (
                                    [0] => Array
                                        (
                                            [check_id] => 4
                                            [statement_id] => 1
                                            [date] => 2018-01-01
                                        )

                                )
                        )
                )
        )

    [1] => Array
        (
            [employee_id] => 2
            [name] => Bob Smith
            [checks] => Array
                (
                    [0] => Array
                        (
                            [check_id] => 3
                            [employee_id] => 2
                            [amount] => 10.00
                            [statements] => Array
                                (
                                    [0] => Array
                                        (
                                            [check_id] => 3
                                            [statement_id] => 1
                                            [date] => 2018-01-02
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [check_id] => 5
                            [employee_id] => 2
                            [amount] => 30.00
                            [statements] => Array
                                (
                                    [0] => Array
                                        (
                                            [check_id] => 5
                                            [statement_id] => 2
                                            [date] => 2018-01-02
                                        )
                                )
                        )
                )
        )
)

Спасибо за вашу помощь.

Ответы [ 2 ]

0 голосов
/ 08 июля 2019

Я пропустил этот код через указанный ввод, и он, похоже, тоже выполняет свою работу. Я преобразовал ваши входные данные print_r в json, используя этот инструмент , что упростило запуск тестовой страницы.

function mergeRelational($source) {
    $objects = [];
    // map the objects by name and id
    foreach($source as $key=>&$objectType) {
        $objectName = $objectType['object_name'];
        $objects[$objectName] = [];
        $idKey = $objectType['id'];

        foreach($objectType['rows'] as &$row) {
            $id = $row[$idKey];
            $objects[$objectName][$id] = &$row;
        }
    }
    // associate the relational data
    foreach($source as $key=>&$objectType) {
        $objectName = $objectType['object_name'];
        $idKey = $objectType['id'];
        $link = $objectType['link_object_name'];
        if(isset($link) && !empty($link)) {     
            $linkedIdColumn = $objectType['link_object_id'];
            foreach($objectType['rows'] as &$row) {
                $id = $row[$idKey];
                $linkedId = $row[$linkedIdColumn];
                $objects[$link][$linkedId][$objectName][$id] = &$row;
            }

        }
    }
    // unset the non-root objects
    foreach($source as $key=>&$objectType) {
        $objectName = $objectType['object_name'];
        if($objectType['level'] !== '0') {
            unset($objects[$objectName]);
        }
    }
    return $objects;
}
0 голосов
/ 08 июля 2019

Для тех, у кого есть тот же вопрос, вот что я придумал:

function merge( $array_of_arrays, $object_name=NULL, $object_id=NULL  )
    {
        $results = array();

        foreach ( $array_of_arrays as $key => $object )
        {
            if( empty($results) && is_null($object_name) )
            {
                foreach($object['rows'] as $k=>$i)
                {
                    $results[$object['object_name']][$k] = $i;
                    $results[$object['object_name']][$k] = array_merge( $results[$object['object_name']][$k], $this->merge( $array_of_arrays, $object['object_name'], $i[$object['id']] ) );
                }
            } elseif( !is_null($object_name) && $object_name == $object['link_object_name'] ) {

                foreach($object['rows'] as $k=>$i)
                {
                    if( $i[$object['link_object_id']] == $object_id ){
                        unset($i[$object['link_object_id']]);
                        $results[$object['object_name']][$k] = $i;
                        $results[$object['object_name']][$k] = array_merge( $results[$object['object_name']][$k], $this->merge( $array_of_arrays, $object['object_name'], $i[$object['id']]  ) );
                    }
                }

            }
        }

        return $results;
    }
...