比较数组中的值并更改值

时间:2017-12-27 09:53:13

标签: php arrays compare

你好 假设我有这个数组:

    array (
  0 => 
  array (
    'hotel_id' => 123,
    'hotel_name' => 'hotel 1',
    'deal_name' => 'deal 1',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-10',
    'plan' => 'B/B',
  ),
  1 => 
  array (
    'hotel_id' => 456,
    'hotel_name' => 'hotel 2',
    'deal_name' => 'deal 1',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-07',
    'plan' => 'H/B',
  ),
  2 => 
  array (
    'hotel_id' => 123,
    'hotel_name' => 'hotel 1',
    'deal_name' => 'deal 1',
    'start_date' => '2018-01-05',
    'end_date' => '2017-02-12',
    'plan' => 'H/B',
  ),
  3 => 
  array (
    'hotel_id' => 666,
    'hotel_name' => 'hotel 3',
    'deal_name' => 'deal 3',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-30',
    'plan' => 'B/B',
  ),
  4 => 
  array (
    'hotel_id' => 123,
    'hotel_name' => 'hotel 1',
    'deal_name' => 'deal 3',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-30',
    'plan' => 'B/B',
  ),
)

我需要运行数组并检查数组中的forech var,如果他的hotel_id和他的deal_name是相同的我想向hotel_id添加99,例如这个数组应该是这样的:

array (
  0 => 
  array (
    'hotel_id' => 12399,
    'hotel_name' => 'hotel 1',
    'deal_name' => 'deal 1',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-10',
    'plan' => 'B/B',
  ),
  1 => 
  array (
    'hotel_id' => 456,
    'hotel_name' => 'hotel 2',
    'deal_name' => 'deal 1',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-07',
    'plan' => 'H/B',
  ),
  2 => 
  array (
    'hotel_id' => 12399,
    'hotel_name' => 'hotel 1',
    'deal_name' => 'deal 1',
    'start_date' => '2018-01-05',
    'end_date' => '2017-02-12',
    'plan' => 'H/B',
  ),
  3 => 
  array (
    'hotel_id' => 666,
    'hotel_name' => 'hotel 3',
    'deal_name' => 'deal 3',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-30',
    'plan' => 'B/B',
  ),
  4 => 
  array (
    'hotel_id' => 123,
    'hotel_name' => 'hotel 1',
    'deal_name' => 'deal 3',
    'start_date' => '2017-12-01',
    'end_date' => '2017-12-30',
    'plan' => 'B/B',
  ),
)

因为数组[0]和数组[2]具有相同的hotel_id和相同的deal_name 但是array [4]只有相同的hotel_id而且feal_name不一样。

这是一个动态数组,因此它可以包含大量数据。 当然可能会有另一个hotel_id有相同的交易。

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么有两个子任务。 1)查找具有相同对“hotel_id和deal_name”的记录。 2)然后改变他们的hotel_id。

我的决定:对于整个阵列的第一次传递,我们将收集有关hotel_id和deal_name的信息。为了保存它,我使用了一个多维数组。

此数组将包含数据

{
  "123": { // hotel_id
    "deal 1": [ // hotel_name
      0,  // keys with this hotel_id and hotel_name
      2
    ],
    "deal 3": [
      4
    ]
  },
  "456": {
    "deal 1": [
      1
    ]
  },
  "666": {
    "deal 3": [
      3
    ]
  }
}

然后,我们遍历多维数组并更改hotel_id。

<?php
$array = array (
    0 =>
        array (
            'hotel_id'   => 123,
            'hotel_name' => 'hotel 1',
            'deal_name'  => 'deal 1',
            'start_date' => '2017-12-01',
            'end_date'   => '2017-12-10',
            'plan'       => 'B/B',
        ),
    1 =>
        array (
            'hotel_id'   => 456,
            'hotel_name' => 'hotel 2',
            'deal_name'  => 'deal 1',
            'start_date' => '2017-12-01',
            'end_date'   => '2017-12-07',
            'plan'       => 'H/B',
        ),
    2 =>
        array (
            'hotel_id'   => 123,
            'hotel_name' => 'hotel 1',
            'deal_name'  => 'deal 1',
            'start_date' => '2018-01-05',
            'end_date'   => '2017-02-12',
            'plan'       => 'H/B',
        ),
    3 =>
        array (
            'hotel_id'   => 666,
            'hotel_name' => 'hotel 3',
            'deal_name'  => 'deal 3',
            'start_date' => '2017-12-01',
            'end_date'   => '2017-12-30',
            'plan'       => 'B/B',
        ),
    4 =>
        array (
            'hotel_id'   => 123,
            'hotel_name' => 'hotel 1',
            'deal_name'  => 'deal 3',
            'start_date' => '2017-12-01',
            'end_date'   => '2017-12-30',
            'plan'       => 'B/B',
        ),
);

$hotelDeals = array();

foreach ($array as $recordKey => $dealData) {
    $hotelId = $dealData['hotel_id'];
    if (!isset($hotelDeals[$hotelId])) {
        $hotelDeals[$hotelId] = array();
    }
    $dealName = $dealData['deal_name'];
    if (!isset($hotelDeals[$hotelId][$dealName])) {
        $hotelDeals[$hotelId][$dealName] = array ($recordKey);
    } else {
        $hotelDeals[$hotelId][$dealName][] = $recordKey;
    }
}

foreach ($hotelDeals as $hotelId => $deals) {
    foreach ($deals as $dealName => $recordKeys) {
        if (count($recordKeys) === 1) {
            continue;
        }
        foreach ($recordKeys as $recordKey) {
            $array[$recordKey]['hotel_id'] = $array[$recordKey]['hotel_id'] . '99';
        }
    }
}