Я использую многомерный php-массив для предоставления данных для генерации html, и я заметил странное поведение, когда два моих подмассива (которые имеют разные ключи) содержат одинаковые значения. Например, этот массив производит дубликаты:PHP foreach дублирует подмассивы с идентичным содержимым
$tableArray = Array(
'rome' => Array(
0 => Array(
'home_prefix' => 'AWE',
'home_number' => '122',
'home_title' => 'Beginning Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to stuff'
)
),
'istanbul' => Array(
0 => Array(
'home_prefix' => 'RPED',
'home_number' => '103',
'home_title' => 'Beginning Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to Greek concepts of stretchiness'
),
1 => Array(
'home_prefix' => 'RPED',
'home_number' => '104',
'home_title' => 'Theory of Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to concepts of stretchiness'
)
),
'new york' => Array(
0 => Array(
'home_prefix' => 'RPED',
'home_number' => '103',
'home_title' => 'Beginning Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to Greek concepts of stretchiness'
),
1 => Array(
'home_prefix' => 'RPED',
'home_number' => '104',
'home_title' => 'Theory of Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to concepts of stretchiness'
)
)
);
foreach ($tableArray as $locationTab):
echo '<p>' . array_search($locationTab, $tableArray) . '</p>';
endforeach;
Выход:
рим
Стамбул
Стамбул
Но когда я добавить еще подмассив поэтому последние два массива не являются идентичны, нет дублирования:
$tableArray = Array(
'rome' => Array(
0 => Array(
'home_prefix' => 'AWE',
'home_number' => '122',
'home_title' => 'Beginning Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to stuff'
)
),
'istanbul' => Array(
0 => Array(
'home_prefix' => 'RPED',
'home_number' => '103',
'home_title' => 'Beginning Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to Greek concepts of stretchiness'
),
1 => Array(
'home_prefix' => 'RPED',
'home_number' => '104',
'home_title' => 'Theory of Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to concepts of stretchiness'
)
),
'new york' => Array(
0 => Array(
'home_prefix' => 'RPED',
'home_number' => '103',
'home_title' => 'Beginning Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to Greek concepts of stretchiness'
),
1 => Array(
'home_prefix' => 'RPED',
'home_number' => '104',
'home_title' => 'Theory of Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to concepts of stretchiness'
),
2 => Array(
'home_prefix' => 'RPED',
'home_number' => '104',
'home_title' => 'Theory of Stretching',
'abroad_prefix' => 'ARCH',
'abroad_number' => '111',
'abroad_title' => 'Intro to concepts of stretchiness'
)
)
);
Выход:
рим
Стамбул
Нью-Йорк
Как я могу решить эту проблему, так Еогеасп не дублирует подмассива? Хотя мои ключи второго уровня будут уникальными, могут быть ситуации, когда значения в двух или более моих массивах второго уровня идентичны.
Я отредактировал мой ответ , Я забыл использовать только что созданную переменную '$ key' –