比较数组中的值并更改值



你好假设我有这个数组:

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

如果他的hotel_id and deal_name与我想将99添加到hotel_id,例如,我需要在数组中运行并检查forech var。

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但是数组[4]只有相同的hotel_id,而不是同一feal_name。

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

请有什么帮助吗?

如果我正确理解了您的问题,则有两个子任务。1)找到具有相同对的" hotel_id and deal_name"的记录。2)然后更改他们的hotel_id。

我的决定:对于整个阵列的第一次通过,我们将收集有关hotel_id and 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';
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新