简体   繁体   English

如何将来自 3 个或更多查询的关系数据合并到一个多维数组中?

[英]How can I merge relational data from 3 or more queries into a multidimensional array?

I need to find a way to pull relational data together from a number of data sources.我需要找到一种方法来从多个数据源中提取关系数据。 Here is how my data structure looks coming out of the data source.这是我的数据结构从数据源中出来的样子。 I want to be able to pull it into a multidimensional array.我希望能够将它拉入一个多维数组。

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
                        )
                )
        )
)

I would like my output to look like this once everything is merged.一旦所有内容合并,我希望我的输出看起来像这样。

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
                                        )
                                )
                        )
                )
        )
)

For anyone who has the same question here is what I came up with:对于任何在这里有同样问题的人,我想出了:

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;
    }

I ran this code through the input given and it seems to do the job also.我通过给定的输入运行了这段代码,它似乎也能完成这项工作。 I converted your print_r input data to json using this tool , which made it easier to get a test page up and running.我使用此工具将您的 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM