使用模式获取所有组合并存储到一个数组

时间:2019-06-27 01:21:36

标签: php arrays

首先,您可以在这里获得我正在处理的数组:

文件#1:包含1家拥有5间客房的酒店

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

文件#2:包含104家酒店

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

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

最可能相关:

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

请下载文件1和2。

在文件#1中。
一旦最小化每个数组,您将获得这种结构。

图#1

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

每个索引,您都可以看到它们在此数组内具有requestRoomIndex索引。

例如,您可以在[0]['roomInfo][requestedRoomIndex]中看到它,其值为3。

所以我将阐述图#1

图#1(已更改)

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

我在图#1 中添加了requestRoomIndex(我只是获得每个索引的requestRoomIndex的不同值)。

在这里,我们将获得由组合(123)配对的索引

我们得到4,这导致我们进入 431,430,421,420 索引。

为什么组合只有123个?

-这是在图1的列表中唯一可见的数字[requestedRoomIndex](已更改)当然,更改请求室后就可以更改。

配对索引= 431,430,421,420 (组合基础)

首先要在我的问题中进行更多阐述:这是我发送给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": []
    }]
}

密切注意roomConfig索引。它包含 3 个数组。

1号房间

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

2号房

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

3号房

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

*组合= 123(我们已请求3个房间)。这表明我被要求提供3个房间。**

根据其[requestedRoomIndex]值降低到 (123)个组合

如您所见,这只是对 API

的一个请求

但是给定的响应根据它们的[requestedRoomIndex]值进行了划分,如图1 [index]中所示。

(合并房间基础)

我们将计算生成的(配对索引)

431,430,421,420 配对索引

这就是我们获得 4(配对索引)的方式。

我要实现的

结构结果:

[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 ]
    },

哪个引导我们

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

我希望我现在能清楚地交付它。感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

这并不是一个完整的答案,但可能会有所帮助。将来会重新访问:

将print_r转换为JSON或数组实际上很有用。这对此很有帮助。 php-print_r-to-json-online

至少可以轻松地从上方使用json将其转换为数组。我做了一些修改。您必须将json / array放入$ 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);

多看一点。仍然不确定要寻找什么,但是我做了一些更改。

结果是:

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
        )

)

结果数组有4个数组,具有我认为您正在寻找的组合??