#php
#php
Вопрос:
Я хочу отфильтровать массив с некоторыми условиями.
Я генерирую случайные наборы отелей для моего туристического портала, где на основе местоположения должны появиться все перестановки отелей.
Условия таковы:- 1. в течение 2 продолжающихся ночей в одном и том же месте должен быть только тот же отель и тип номера.
Мой подход (я использую array_filter()):
public static function my_function($item_values)
{
for($i=0;$i<count($item_values);$i ){
if($i>0){
$j = $i-1;
$preData = explode('-',$item_values[$j]);
$data = explode('-',$item_values[$i]);
if($data[0] == $preData[0]){
if($data[1] == $preData[1]){
if($data[2] == $preData[2]){
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
} else {
return FALSE;
}
} else {
return true;
}
}
}
МОЙ массив :
Array
(
[0] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-4-13
)
[1] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-271
)
[2] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-269
)
[3] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-202
[4] => 1-4-13
)
[4] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-202
[4] => 1-100-271
)
[5] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-202
[4] => 1-100-269
)
[6] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-76-203
[4] => 1-4-13
)
[7] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-76-203
[4] => 1-100-271
)
[8] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-76-203
[4] => 1-100-269
)
[9] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-56-116
[4] => 1-4-13
)
[10] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-56-116
[4] => 1-100-271
)
[11] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-56-116
[4] => 1-100-269
)
[12] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-295
[4] => 1-4-13
)
[13] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-295
[4] => 1-100-271
)
[14] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-295
[4] => 1-100-269
)
[15] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-55-84
[4] => 1-4-13
)
[16] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-55-84
[4] => 1-100-271
)
[17] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-55-84
[4] => 1-100-269
)
[18] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-115
[4] => 1-4-13
)
[19] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-115
[4] => 1-100-271
)
[20] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-115
[4] => 1-100-269
)
[21] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-4-13
)
[22] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-271
)
[23] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-269
)
[24] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-76-203
[4] => 1-4-13
)
[25] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-76-203
[4] => 1-100-271
)
[26] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-76-203
[4] => 1-100-269
)
[27] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-56-116
[4] => 1-4-13
)
[28] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-56-116
[4] => 1-100-271
)
[29] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-56-116
[4] => 1-100-269
)
[30] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-295
[4] => 1-4-13
)
[31] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-295
[4] => 1-100-271
)
[32] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-295
[4] => 1-100-269
)
[33] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-55-84
[4] => 1-4-13
)
[34] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-55-84
[4] => 1-100-271
)
[35] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-55-84
[4] => 1-100-269
)
[36] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-115
[4] => 1-4-13
)
[37] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-115
[4] => 1-100-271
)
[38] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-115
[4] => 1-100-269
)
[39] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-202
[4] => 1-4-13
)
[40] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-202
[4] => 1-100-271
)
[41] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-202
[4] => 1-100-269
)
[42] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-76-203
[4] => 1-4-13
)
[43] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-76-203
[4] => 1-100-271
)
[44] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-76-203
[4] => 1-100-269
)
[45] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-56-116
[4] => 1-4-13
)
[46] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-56-116
[4] => 1-100-271
)
[47] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-56-116
[4] => 1-100-269
)
[48] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-295
[4] => 1-4-13
)
[49] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-295
[4] => 1-100-271
)
[50] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-50-295
[4] => 1-100-269
)
[51] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-55-84
[4] => 1-4-13
)
[52] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-55-84
[4] => 1-100-271
)
[53] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-55-84
[4] => 1-100-269
)
[54] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-115
[4] => 1-4-13
)
[55] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-115
[4] => 1-100-271
)
[56] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-115
[4] => 1-100-269
)
[57] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-202
[4] => 1-4-13
)
[58] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-202
[4] => 1-100-271
)
[59] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-202
[4] => 1-100-269
)
[60] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-76-203
[4] => 1-4-13
)
[61] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-76-203
[4] => 1-100-271
)
[62] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-76-203
[4] => 1-100-269
)
[63] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-56-116
[4] => 1-4-13
)
[64] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-56-116
[4] => 1-100-271
)
[65] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-56-116
[4] => 1-100-269
)
[66] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-295
[4] => 1-4-13
)
[67] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-295
[4] => 1-100-271
)
[68] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-50-295
[4] => 1-100-269
)
[69] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-55-84
[4] => 1-4-13
)
[70] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-55-84
[4] => 1-100-271
)
[71] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-55-84
[4] => 1-100-269
)
[72] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-115
[4] => 1-4-13
)
[73] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-115
[4] => 1-100-271
)
[74] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-115
[4] => 1-100-269
)
[75] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-202
[4] => 1-4-13
)
[76] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-202
[4] => 1-100-271
)
[77] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-202
[4] => 1-100-269
)
[78] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-76-203
[4] => 1-4-13
)
[79] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-76-203
[4] => 1-100-271
)
[80] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-76-203
[4] => 1-100-269
)
[81] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-56-116
[4] => 1-4-13
)
[82] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-56-116
[4] => 1-100-271
)
[83] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-56-116
[4] => 1-100-269
)
[84] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-295
[4] => 1-4-13
)
[85] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-295
[4] => 1-100-271
)
[86] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-295
[4] => 1-100-269
)
[87] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-55-84
[4] => 1-4-13
)
[88] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-55-84
[4] => 1-100-271
)
[89] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-55-84
[4] => 1-100-269
)
[90] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-115
[4] => 1-4-13
)
[91] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-115
[4] => 1-100-271
)
[92] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-115
[4] => 1-100-269
)
[93] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-202
[4] => 1-4-13
)
[94] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-202
[4] => 1-100-271
)
[95] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-202
[4] => 1-100-269
)
[96] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-76-203
[4] => 1-4-13
)
[97] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-76-203
[4] => 1-100-271
)
[98] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-76-203
[4] => 1-100-269
)
[99] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-56-116
[4] => 1-4-13
)
[100] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-56-116
[4] => 1-100-271
)
[101] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-56-116
[4] => 1-100-269
)
[102] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-295
[4] => 1-4-13
)
[103] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-295
[4] => 1-100-271
)
[104] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-50-295
[4] => 1-100-269
)
[105] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-55-84
[4] => 1-4-13
)
[106] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-55-84
[4] => 1-100-271
)
[107] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-55-84
[4] => 1-100-269
)
[108] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-4-13
)
[109] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-271
)
[110] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-269
)
[111] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-202
[4] => 1-4-13
)
[112] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-202
[4] => 1-100-271
)
[113] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-202
[4] => 1-100-269
)
[114] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-76-203
[4] => 1-4-13
)
[115] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-76-203
[4] => 1-100-271
)
[116] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-76-203
[4] => 1-100-269
)
[117] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-56-116
[4] => 1-4-13
)
[118] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-56-116
[4] => 1-100-271
)
[119] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-56-116
[4] => 1-100-269
)
[120] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-295
[4] => 1-4-13
)
[121] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-295
[4] => 1-100-271
)
[122] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-295
[4] => 1-100-269
)
[123] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-55-84
[4] => 1-4-13
)
[124] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-55-84
[4] => 1-100-271
)
[125] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-55-84
[4] => 1-100-269
)
[126] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-115
[4] => 1-4-13
)
[127] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-115
[4] => 1-100-271
)
[128] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-115
[4] => 1-100-269
)
[129] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-4-13
)
[130] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-271
)
[131] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-269
)
[132] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-76-203
[4] => 1-4-13
)
[133] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-76-203
[4] => 1-100-271
)
[134] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-76-203
[4] => 1-100-269
)
[135] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-56-116
[4] => 1-4-13
)
[136] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-56-116
[4] => 1-100-271
)
[137] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-56-116
[4] => 1-100-269
)
[138] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-295
[4] => 1-4-13
)
[139] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-295
[4] => 1-100-271
)
[140] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-295
[4] => 1-100-269
)
[141] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-55-84
[4] => 1-4-13
)
[142] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-55-84
[4] => 1-100-271
)
[143] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-55-84
[4] => 1-100-269
)
[144] => Array
(
[1] => 1-100-271
[2] => 2-76-203
[3] => 2-50-115
[4] => 1-4-13
)
[145] => Array
(
[1] => 1-100-271
[2] => 2-76-203
[3] => 2-50-115
[4] => 1-100-271
)
...
)
Итак, в элементе массива [1] => 1-100-269 отображается как:
[1] == Ключ и ночь. 1 — идентификатор местоположения. 100 — идентификатор отеля, 269 — идентификатор типа номера.
я ожидаю, что массив будет иметь только тот же отель и тип номера в одних и тех же местах на разные ночи. Но я получаю все 323 элемента массива.
Приветствуется любая помощь.
Мой ожидаемый массив:
Array
(
[0] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-4-13
)
[1] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-271
)
[2] => Array
(
[1] => 1-4-13
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-269
)
[3] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-4-13
)
[4] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-271
)
[5] => Array
(
[1] => 1-4-13
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-269
)
[6] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-76-203
[4] => 1-4-13
)
[7] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-76-203
[4] => 1-100-271
)
[8] => Array
(
[1] => 1-4-13
[2] => 2-76-203
[3] => 2-76-203
[4] => 1-100-269
)
[9] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-56-116
[4] => 1-4-13
)
[10] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-56-116
[4] => 1-100-271
)
[11] => Array
(
[1] => 1-4-13
[2] => 2-56-116
[3] => 2-56-116
[4] => 1-100-269
)
[12] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-295
[4] => 1-4-13
)
[13] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-295
[4] => 1-100-271
)
[14] => Array
(
[1] => 1-4-13
[2] => 2-50-295
[3] => 2-50-295
[4] => 1-100-269
)
[15] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-55-84
[4] => 1-4-13
)
[16] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-55-84
[4] => 1-100-271
)
[17] => Array
(
[1] => 1-4-13
[2] => 2-55-84
[3] => 2-55-84
[4] => 1-100-269
)
[18] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-4-13
)
[19] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-271
)
[20] => Array
(
[1] => 1-100-271
[2] => 2-50-115
[3] => 2-50-115
[4] => 1-100-269
)
[21] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-4-13
)
[22] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-271
)
[23] => Array
(
[1] => 1-100-271
[2] => 2-50-202
[3] => 2-50-202
[4] => 1-100-269
)
)
Комментарии:
1. каков ваш ожидаемый результат? и можете ли вы предоставить свой массив в формате JSON, чтобы мы могли попробовать с нашей стороны
2. пожалуйста, проверьте мою правку. я обновил свой ожидаемый массив.