PHP - Merge-down multidimensional arrays by named key

六眼飞鱼酱① 提交于 2019-12-25 00:14:16

问题


I have the following array (which contains results of multiple database queries - in the example only 2 but can be more. The merging needs to be done by a specified column (in this case the date column). Let's call this merge_by column.

 Array
(
[0] => Array
    (
        [0] => Array
            (
                [date] => 2011-01-17
                [col-1] => 58
                [col-2] => 54
            )

        [1] => Array
            (
                [date] => 2011-01-19
                [col-1] => 50
                [col-2] => 61
            )

        [2] => Array
            (
                [date] => 2011-01-20
                [col-1] => 44
                [col-2] => 22
            )

        [3] => Array
            (
                [date] => 
                [col-1] => 448
                [col-2] => 196

    )

[1] => Array
    (
        [0] => Array
            (
                [date] => 2011-01-17
                [col-3] => 1489
            )

        [1] => Array
            (
                [date] => 2011-01-18
                [col-3] => 1534 
            )

        [2] => Array
            (
                [date] => 
                [col-3] => 1534
            )

    )

)

And I'm trying to achieve

Array
    (
        [0] => Array
            (
                [date] => 2011-01-17
                [col-1] => 58
                [col-2] => 54
                [col-3] => 1489
            )

        [1] => Array
            (
                [date] => 2011-01-18
                [col-1] =>
                [col-2] =>
                [col-3] => 1534
            )

        [2] => Array
            (
                [date] => 2011-01-19
                [col-1] => 50
                [col-2] => 61
                [col-3] =>
            )

        [3] => Array
            (
                [date] => 2011-01-20
                [col-1] => 44
                [col-2] => 22
                [col-3] =>
            )

        [4] => Array
            (
                [date] => 
                [col-1] => 448
                [col-2] => 196
                [col-3] => 1534

    )

Things to consider:

  • merge_by can take null or empty string values
  • merge_by will not have same values in the arrays to be merged
  • the number of results in the 2 queries will differ and the merge_by values can differ, so one might encounter
    $arr[0][1][date] != $arr[1][1][date]
  • LE: except the merge_by column, all the other columns will differ from each other, so you won't have $arr[0][0][col-2] and $arr[1][0][col-2]

So the purpose of this is to merge the columns of some queries by a common column.
Too much data from too many databases from too many columns to make it from SQL only. I know... pre-calculate the values :) ... not an option now.

I have managed to make it work when the number of columns and the indexes of these match. Have since tried many options so right now I have a version unworthy of adding it as example.

So does anyone any idea of a function that can achieve the above?

Thanks


回答1:


First, I'm going to assume your data is sorted by date, since you can do it in SQL and it's sorted in your example. You need to walk through all of your sets at the same time, merging as you go.

$merged = array();
$i_first = 0;
$i_second = 0;

$count_first = count($data[0]);
$cound_second = count($data[1]);

while ($i_first < $count_first || $i_second < $count_second)
{
    // this comparison depends on what your merge_by is
    $diff = strtotime($data[$i_first]['date']) - strtotime($data[$i_second]['date']);

    if ($diff == 0)
    {
        $merged[] = array_merge($data[$i_first], $data[$i_second]);
        $i_first++;
        $i_second++;
    }
    elseif ($diff < 0) // first date is earlier
    {
        $i_first++;
    }
    else  // second date earlier
    {
        $i_second++;
    }
}

Now your $merged array should have what you want. Note that this solution assumes you don't have duplicate date rows in one chunk, or it would overwrite the existing results. Also, you could expand to have more than two data sets if you want.



来源:https://stackoverflow.com/questions/4748292/php-merge-down-multidimensional-arrays-by-named-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!