比较数组中的值并更改值[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了比较数组中的值并更改值[关闭]相关的知识,希望对你有一定的参考价值。

你好假设我有这个数组:

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

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

请帮忙吗?

答案

如果我正确理解了您的问题,那么有两个子任务。 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';
        }
    }
}

以上是关于比较数组中的值并更改值[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

根据数组中的值检查值并使用 PHP 返回键

如何遍历 2 个文件中的行,比较值并在满足条件时更新文件中的值?

更改 Json 数组的值并放入 Hiddenfield

比较一个结构的两个值并在 UITableView swift 4.0 中获取额外的单元格

比较不同时间戳的值并确定它们是不是已更改

比较 2 个表中的值并生成具有差异的新表