Hello Suppose i have this 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',
),
)
i need to run through array and check forech var in array if his hotel_id and his deal_name is the same i want to add 99 to hotel_id for example this array should look like this:
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',
),
)
Because the array[0] and array[2] have the same hotel_id and the same deal_name but array[4] have only the same hotel_id and not same feal_name.
this is a dynamic array so it can contain lots of data. Of course there could be another hotel_id with the same deals.
Please any help?
If I correctly understood your question, then there are two subtasks. 1) Find records with identical pairs "hotel_id and deal_name". 2) Then change their hotel_id.
My decision: for the first pass on the entire array, we will collect information about the hotel_id and deal_name. To save it, I use a multidimensional array.
This array will contain data
{
"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
]
}
}
Then, we go over the multidimensional array and change the 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';
}
}
}