0

First of all you can get the array that i was working on here :

File # 1: Contains 1 hotel with 5rooms

https://drive.google.com/file/d/1XO6tMv0txfI2gLIrDYfy7jmXCPTWfIiC/view?usp=sharing

File # 2: Contains 104 hotels

https://drive.google.com/file/d/161SZ_ZP9NbcLUFvlFNaPNDmezmfHX8NI/view?usp=sharing

index [accommodationInfo] = per hotel
index [roomStay] = per room

Most likely related:

PHP: How to get all possible combinations of 1D array?

Please download file 1 and 2.

In file # 1.
Once you minimize each array you will get this kind of structure.

Figure # 1

[indexes]
[0]     => Array ...
[1]     => Array ...
[2]     => Array ...
[3]     => Array ...
[4]     => Array ...

each of this indexes you can see that they have requestedRoomIndex index inside of this array.

you can see it in for example [0]['roomInfo][requestedRoomIndex] which has a value of 3.

So i will ellaborate the Figure # 1

Figure # 1 (changed)

[index]                 [requestedRoomIndex]
[0]     => Array ...        => 3,
[1]     => Array ...        => 3,
[2]     => Array ...        => 2,
[3]     => Array ...        => 2,
[4]     => Array ...        => 1

I was added requestedRoomIndex in figure # 1 (i just get the different value of the requestedRoomIndex per indexes).

Here we will get the indexes that are paired by the Combination(123):

We get 4 which leads us to 431,430,421,420 indexes.

Why Combination is only 123?

- That is the only numbers that are seen in list of Figure # 1 [requestedRoomIndex] (changed) of course it can be change once the request room was changed.

Pairedindexes = 431,430,421,420 (base in Combination)

Just to ellaborate more in my question at first: This is the request that i was send to the API

"requestBody":
{
    "accommodationSubTypes":["Hotel","Motel"],
    "countryCode": "SGP",
    "cityCode": "SGP-SNGP000",
    "hotelCode": "",
    "checkIn": "2019-07-23",
    "checkOut": "2019-07-25",
    "paxNationality":"PH",
    "roomConfig": [{
        "adultCount": 1,
        "childAges": [5,12]
    },
    {
        "adultCount": 2,
        "childAges": []
    },
    {
        "adultCount": 1,
        "childAges": []
    }]
}

keep on eye on roomConfig index. It contains of 3 array.

Room # 1

{
    "adultCount": 1,
    "childAges": [5,12]
}

Room # 2

{
    "adultCount": 2,
    "childAges": []
}

Room # 3

{
    "adultCount": 1,
    "childAges": []
}

*Combination = 123 (we have requested 3 rooms). Which indicates that i was requested a 3 room.**

Fall down to (123) combinations base on their [requestedRoomIndex] value.

As you can see this is only a 1 request on API

But the response given was divided according to their [requestedRoomIndex] value as you seen in Figure # 1 [index].

(Merging of rooms base in the Combination)

We will count the generated (Pairedindexes).

(431,430,421,420 Pairedindexes)

So this is how we get 4 (Pairedindexes).

Structure Result that i want to achieve:

[0] => 
    {
        [the contents of the index 4 in our array ]
    },
    {
        [the contents of the index 3 in our array ]
    },
    {
        [the contents of the index 1 in our array ]
    },
[1] => 
    {
        [the contents of the index 4 in our array ]
    },
    {
        [the contents of the index 2 in our array ]
    },
    {
        [the contents of the index 1 in our array ]
    },
[2] => 
    {
        [the contents of the index 4 in our array ]
    },
    {
        [the contents of the index 3 in our array ]
    },
    {
        [the contents of the index 0 in our array ]
    },
[3] => 
    {
        [the contents of the index 4 in our array ]
    },
    {
        [the contents of the index 2 in our array ]
    },
    {
        [the contents of the index 0 in our array ]
    },

Which leads us to

[Generated Paired Combination indexes]
[0] => [{4},{3},{1}]
[1] => [{4},{2},{1}]
[2] => [{4},{3},{0}]
[4] => [{4},{2},{0}]

I was hoping that i was deliver it clearly now. Thank you for any help.

Mike
  • 3
  • 4
  • welcome to Stack Overflow! you've provided some useful details, but your question is still missing key information; please see SO's [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimum, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) for ways to improve your question and make it more practical for others to help you – landru27 Jun 27 '19 at 02:29
  • thank you @landru27 sorry that im not good in posting a questions but im practicing and formatting my question again so that the main focus would be seen. – Mike Jun 27 '19 at 03:01
  • This is the second time I've seen this assignment pop up this week, and it still makes absolutely no sense to me. Can someone explain what exactly they're trying to teach with this? Maybe someone could just post the question verbatim? (Of course, that would rightly get down voted and put on hold, but at least then I might be able to figure out what in the heck this problem is supposed to accomplish.) – Tim Morton Jun 27 '19 at 03:29
  • @TimMorton im sorry im not good enough in generating questions to help the community. – Mike Jun 27 '19 at 03:49

1 Answers1

0

Not really a complete answer, but might help. Will revisit in future:

It is actually useful to convert your print_r to JSON or an array. This is helpful for that. php-print_r-to-json-online

That at least makes it easy to convert that to an array with the json from above. I made some modifications. You would have to put your json / array into $array.

$array = json_decode($json, true);

$result = array_map( function($a) { return $a['roomInfo']['requestedRoomIndex']; }, $array);
print_r($result);
$uniquevalues = array_unique($result); 
$configarray = [];
foreach ($uniquevalues as $key => $value) {
    $configarray[$value] = array_filter($result, function($v, $k) use ($value) { if ($v == $value) return $v ;}, ARRAY_FILTER_USE_BOTH);
}

function get_combinations($arrays) {
    $result = array(array());
    foreach ($arrays as $property => $property_values) {
        $tmp = array();
        foreach ($result as $result_item) {
            foreach ($property_values as $property_key => $property_value) {
                $tmp[] = $result_item + array(current($property_values) => $property_key);
            }
        }
        $result = $tmp;
    }
    return $result;
}

$results = [];
foreach(get_combinations($configarray) as $combo) {
    // $results[] = implode($combo, "-");
    $results[] = $combo;
}
print_r($results);

Did a little more looking. Still not absolutely certain what you are looking for, but I made some changes.

The results of that are:

Array
(
    [0] => 3
    [1] => 3
    [2] => 2
    [3] => 2
    [4] => 1
)
Array
(
    [0] => Array
        (
            [3] => 0
            [2] => 2
            [1] => 4
        )

    [1] => Array
        (
            [3] => 0
            [2] => 3
            [1] => 4
        )

    [2] => Array
        (
            [3] => 1
            [2] => 2
            [1] => 4
        )

    [3] => Array
        (
            [3] => 1
            [2] => 3
            [1] => 4
        )

)

The results array has 4 arrays, having the combinations that I think you are looking for ??

SScotti
  • 2,158
  • 4
  • 23
  • 41
  • few changes that i have been changed in my process and it was already resolve my problem – Mike Jun 27 '19 at 06:25