将多维关联数组与匹配的键值对组合在一起

时间:2016-03-11 13:12:58

标签: php arrays multidimensional-array

 [traffic] => Array
    (
        [0] => Array
            (
                [id] => 1
                [visitors] => 310
                [pageviews] => 1333
                [created_date] => 2016-03-09
            )

        [1] => Array
            (
                [id] => 2
                [visitors] => 374
                [pageviews] => 1010
                [created_date] => 2016-03-10
            )

        [2] => Array
            (
                [id] => 3
                [visitors] => 143
                [pageviews] => 617
                [created_date] => 2016-03-11
            )

    )

[source] => Array
    (
        [0] => Array
            (
                [created_date] => 2016-03-09
                [scount] => 368
            )

        [1] => Array
            (
                [created_date] => 2016-03-10
                [scount] => 550
            )

        [2] => Array
            (
                [created_date] => 2016-03-11
                [scount] => 238
            )

    )

我有两个多维数组,我想将两个数组合并为一个匹配的created_date值,结果应该是这样的,

阵 (

[0] => Array
    (
        [created_date] => 2016-03-09
        [id] => 1
        [visitors] => 310
        [pageviews] => 1333
        [scount] => 368
    )

[1] => Array
    (
        [created_date] => 2016-03-10
        [id] => 2
        [visitors] => 374
        [pageviews] => 1010
        [scount] => 550
    )

[2] => Array
    (
        [created_date] => 2016-03-11
        [id] => 3
        [visitors] => 143
        [pageviews] => 617
        [scount] => 238
    )

3 个答案:

答案 0 :(得分:0)

$traffic = []; //...
$source = []; // ...

foreach($traffic as $key => $value)
{
    if(isset($source[$key]))
    {
        $token = $source[$key];
        foreach($token as $keyy => $valuee)
        {
            if(isset($traffic[$key][$keyy]))
            {
                // Collision handling, if any ...
                $traffic[$key][$keyy] = $valuee;
            }
            else $traffic[$key][$keyy] = $valuee;
        }
    }
}

答案 1 :(得分:0)


以下代码可以解决问题。

解决方案:

# I split your array into 2 parts ($traffic = $your_array['traffic'])
$traffic = array(
  array(
    id => 1,
    visitors => 310,
    pageviews => 1333,
    created_date => '2016-03-09'
  ),
  array(
    id => 2,
    visitors => 374,
    pageviews => 1010,
    created_date => '2016-03-10'
  ),
  array(
    id => 3,
    visitors => 143,
    pageviews => 617,
    created_date => '2016-03-11'
  )
);

# I split your array into 2 parts ($source = $your_array['source'])
$source = array(
  array (
    created_date => '2016-03-09',
    scount => 368
  ),
  array (
    created_date => '2016-03-10',
    scount => 550
  ),
  array (
    created_date => '2016-03-11',
    scount => 238
  )
);

# copy the traffic array cause we want to merge the new data into it
$result = $traffic;

# loop over the traffic array
foreach ($traffic as $k => $t) {

  # loop over the source
  foreach ($source as $s) {

    # try to find a match
    if ($t['created_date'] === $s['created_date']) {

      # add data to result
      $result[$k]['scount'] = $s['scount'];

      # we exit the inner foreach-loop here as there is only 1 match
      break;
    }
  }
}

# print the result
echo '<pre>'; print_r($result); echo '</pre>';

结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [visitors] => 310
            [pageviews] => 1333
            [created_date] => 2016-03-09
            [scount] => 368
        )

    [1] => Array
        (
            [id] => 2
            [visitors] => 374
            [pageviews] => 1010
            [created_date] => 2016-03-10
            [scount] => 550
        )

    [2] => Array
        (
            [id] => 3
            [visitors] => 143
            [pageviews] => 617
            [created_date] => 2016-03-11
            [scount] => 238
        )

)

您可以在此处测试:http://www.writephponline.com

答案 2 :(得分:0)

以下是您的解决方案 : -

$arr1 = $arr1['traffic']; // assign key traffic record to array1
$arr2 = $arr2['source'];  // assign key source record to array2
$result = [];
foreach($arr1 as $key=>$value){     
    $result[$key] = $value;
    // find created_date in second array
    $keyOfSecondArr = array_search($value['created_date'], array_column($arr2, 'created_date'));
    $result[$key]['scount'] = $arr2[$keyOfSecondArr]['scount'];
}
echo '<pre>'; print_r($result);

<强>输出: -

Array
(
    [0] => Array
        (
            [id] => 1
            [visitors] => 310
            [pageviews] => 1333
            [created_date] => 2016-03-09
            [scount] => 368
        )

    [1] => Array
        (
            [id] => 2
            [visitors] => 374
            [pageviews] => 1010
            [created_date] => 2016-03-10
            [scount] => 550
        )

    [2] => Array
        (
            [id] => 3
            [visitors] => 143
            [pageviews] => 617
            [created_date] => 2016-03-11
            [scount] => 238
        )

)