重建数组以添加第二个数组

时间:2016-11-01 18:43:32

标签: php arrays

如何使用Array1和Array2获取通缉结果?

数组1

array(
    (int) 0 => '37',
    (int) 1 => '38'
)

ARRAY2

array(
    (int) 0 => array(
        'ParentKey' => array(
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    ),
    (int) 1 => array(
        'ParentKey' => array(
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
)

通缉结果

array(
    (int) 0 => array(
        'ParentKey' => array(
            'Array1Key' => 37,
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    ),
    (int) 1 => array(
        'ParentKey' => array(
            'Array1Key' => 37,
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
    (int) 2 => array(
        'ParentKey' => array(
            'Array1Key' => 38,
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    ),
    (int) 3 => array(
        'ParentKey' => array(
            'Array1Key' => 38,
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
)

我尝试了几件事,但这让我最接近......

PHP

$data = array();
foreach($Array1 as $id)
{
    foreach($Array2 as $Array1Result)
    {
        $data[]['ParentKey'] = array(
            'Array1Key' => $id,
            $Array1Result['ParentKey']
        );
    }
}

结果

array(
  (int) 0 => array(
    'ParentKey' => array(
        'Array1Key' => '37',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    )
  ),
  (int) 1 => array(
    'ParentKey' => array(
        'Array1Key' => '37',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
  ),
  (int) 2 => array(
    'ParentKey' => array(
        'Array1Key' => '38',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    )
  ),
  (int) 3 => array(
    'ParentKey' => array(
        'Array1Key' => '38',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
  )
)

1 个答案:

答案 0 :(得分:2)

你得到的正是你所做的。你把一个数组放在一个数组中。但是你需要将两个数组合并在一起。第一个是SELECT Activity.EmployeeID, SHIFTS.ThisShiftLogin, Activity.EventName, Activity.EventTime FROM @T Activity INNER JOIN ( SELECT ShiftStart.EmployeeID, ThisShiftLogin=ShiftStart.LoginDate, NextShiftLogin=NextShiftStart.LoginDate FROM ( SELECT EmployeeID, LoginDate=MIN(EventTime), RowID=ROW_NUMBER()OVER( PARTITION BY EmployeeID ORDER BY MIN(EventTime)) FROM @T WHERE EventName='Log In' GROUP BY EmployeeID, DATEADD(d, 0, DATEDIFF(d, 0, EventTime)) )AS ShiftStart LEFT OUTER JOIN ( SELECT EmployeeID, LoginDate=MIN(EventTime), RowID=ROW_NUMBER()OVER( PARTITION BY EmployeeID ORDER BY MIN(EventTime)) FROM @T WHERE EventName='Log In' GROUP BY EmployeeID, DATEADD(d, 0, DATEDIFF(d, 0, EventTime)) )AS NextShiftStart ON ShiftStart.RowID=NextShiftStart.RowID-1 )AS SHIFTS ON Activity.EmployeeID=SHIFTS.EmployeeID AND ( (SHIFTS.NextShiftLogin IS NULL AND Activity.EventTime >= SHIFTS.ThisShiftLogin) OR (NOT SHIFTS.NextShiftLogin IS NULL AND Activity.EventTime >= SHIFTS.ThisShiftLogin AND Activity.EventTime < SHIFTS.NextShiftLogin ) ) ,第二个是旧数组。您可以使用array_merge实现此目的。

Array1Key