簡體   English   中英

如何將來自 3 個或更多查詢的關系數據合並到一個多維數組中?

[英]How can I merge relational data from 3 or more queries 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
                        )
                )
        )
)

一旦所有內容合並,我希望我的輸出看起來像這樣。

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

對於任何在這里有同樣問題的人,我想出了:

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

我通過給定的輸入運行了這段代碼,它似乎也能完成這項工作。 我使用此工具將您的 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