Как ни странно, следующий код возвращает ошибку, сообщающую, что ключ массива 11 или 12 не существует для playerPool[$x]['gender']. При дампе массива $playersPool он обычно возвращает значение 9:
array(9) {
[0]=>
array(20) {
["id"]=>
int(26)
["tournament"]=>
int(4)
["player"]=>
int(26)
["no_of_assigned_matches"]=>
int(1)
}
[1]=>
array(20) {
["id"]=>
int(25)
["tournament"]=>
int(4)
["player"]=>
int(25)
["no_of_assigned_matches"]=>
int(1)
}
[2]=>
array(20) {
["id"]=>
int(24)
["tournament"]=>
int(4)
["player"]=>
int(24)
["no_of_assigned_matches"]=>
int(1)
}
[3]=>
array(20) {
["id"]=>
int(80)
["tournament"]=>
int(4)
["player"]=>
int(80)
["no_of_assigned_matches"]=>
int(1)
}
[4]=>
array(20) {
["id"]=>
int(33)
["tournament"]=>
int(4)
["player"]=>
int(33)
["no_of_assigned_matches"]=>
int(1)
}
[5]=>
array(20) {
["id"]=>
int(75)
["tournament"]=>
int(4)
["player"]=>
int(75)
["no_of_assigned_matches"]=>
int(1)
}
[6]=>
array(20) {
["id"]=>
int(31)
["tournament"]=>
int(4)
["player"]=>
int(31)
["no_of_assigned_matches"]=>
int(1)
}
[7]=>
array(20) {
["id"]=>
int(29)
["tournament"]=>
int(4)
["player"]=>
int(29)
["no_of_assigned_matches"]=>
int(0)
}
[8]=>
array(20) {
["id"]=>
int(27)
["tournament"]=>
int(4)
["player"]=>
int(27)
["no_of_assigned_matches"]=>
int(0)
}
}
Как это возможно, что я получаю сообщение об ошибке, если $x не может быть больше количества элементов в массиве. Делает ли что-нибудь uniquemultidimArray с указателями?
или функция shuffle (которая лучше, чем функция PHP shuffle)?
$playersOtherLevel = Player::getPlayersByLevelAndOpponent($tourid, $playerid, $otherLevel, $maxRounds-1);
if(!($playersOtherLevel)){
echo'no otherlevelplayers';
die();
}
$playersOtherLevels = Common::array_shuffle($playersOtherLevel); // shuffle the order to pick different players each time
$otherLevelCount = ($count == 0 ? count($playersOtherLevels): $count);
for($v=0;$v < $otherLevelCount;$v++){
$players[] = $playersOtherLevels[$v];
}
$playersPool = Common::uniqueMultidimArray($players, 'id');
}
$ppoolMales = $ppoolFemales = [];
// split pool into male and female pools
$counter = count($playersPool);
for ($x = 0; $x < $counter; $x++){
if($playersPool[$x]['gender'] == 1) {
$ppoolMales[] = $playersPool[$x]['player'];
}
else {
$ppoolFemales[] = $playersPool[$x]['player'];
}
}
return array($ppoolMales,$ppoolFemales,$playersPool);
уникальная функция multidimArray:
public static function uniqueMultidimArray($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
array_shuffle:
public static function array_shuffle($array){
// shuffle using Fisher-Yates
$i = count($array);
while(--$i){
$j = mt_rand(0,$i);
if($i != $j){
// swap items
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... rray-index
Счетчик цикла for выдает ошибку для индекса массива ⇐ Php
Кемеровские программисты php общаются здесь
-
Anonymous
1770924041
Anonymous
Как ни странно, следующий код возвращает ошибку, сообщающую, что ключ массива 11 или 12 не существует для playerPool[$x]['gender']. При дампе массива $playersPool он обычно возвращает значение 9:
array(9) {
[0]=>
array(20) {
["id"]=>
int(26)
["tournament"]=>
int(4)
["player"]=>
int(26)
["no_of_assigned_matches"]=>
int(1)
}
[1]=>
array(20) {
["id"]=>
int(25)
["tournament"]=>
int(4)
["player"]=>
int(25)
["no_of_assigned_matches"]=>
int(1)
}
[2]=>
array(20) {
["id"]=>
int(24)
["tournament"]=>
int(4)
["player"]=>
int(24)
["no_of_assigned_matches"]=>
int(1)
}
[3]=>
array(20) {
["id"]=>
int(80)
["tournament"]=>
int(4)
["player"]=>
int(80)
["no_of_assigned_matches"]=>
int(1)
}
[4]=>
array(20) {
["id"]=>
int(33)
["tournament"]=>
int(4)
["player"]=>
int(33)
["no_of_assigned_matches"]=>
int(1)
}
[5]=>
array(20) {
["id"]=>
int(75)
["tournament"]=>
int(4)
["player"]=>
int(75)
["no_of_assigned_matches"]=>
int(1)
}
[6]=>
array(20) {
["id"]=>
int(31)
["tournament"]=>
int(4)
["player"]=>
int(31)
["no_of_assigned_matches"]=>
int(1)
}
[7]=>
array(20) {
["id"]=>
int(29)
["tournament"]=>
int(4)
["player"]=>
int(29)
["no_of_assigned_matches"]=>
int(0)
}
[8]=>
array(20) {
["id"]=>
int(27)
["tournament"]=>
int(4)
["player"]=>
int(27)
["no_of_assigned_matches"]=>
int(0)
}
}
Как это возможно, что я получаю сообщение об ошибке, если $x не может быть больше количества элементов в массиве. Делает ли что-нибудь uniquemultidimArray с указателями?
или функция shuffle (которая лучше, чем функция PHP shuffle)?
$playersOtherLevel = Player::getPlayersByLevelAndOpponent($tourid, $playerid, $otherLevel, $maxRounds-1);
if(!($playersOtherLevel)){
echo'no otherlevelplayers';
die();
}
$playersOtherLevels = Common::array_shuffle($playersOtherLevel); // shuffle the order to pick different players each time
$otherLevelCount = ($count == 0 ? count($playersOtherLevels): $count);
for($v=0;$v < $otherLevelCount;$v++){
$players[] = $playersOtherLevels[$v];
}
$playersPool = Common::uniqueMultidimArray($players, 'id');
}
$ppoolMales = $ppoolFemales = [];
// split pool into male and female pools
$counter = count($playersPool);
for ($x = 0; $x < $counter; $x++){
if($playersPool[$x]['gender'] == 1) {
$ppoolMales[] = $playersPool[$x]['player'];
}
else {
$ppoolFemales[] = $playersPool[$x]['player'];
}
}
return array($ppoolMales,$ppoolFemales,$playersPool);
уникальная функция multidimArray:
public static function uniqueMultidimArray($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
array_shuffle:
public static function array_shuffle($array){
// shuffle using Fisher-Yates
$i = count($array);
while(--$i){
$j = mt_rand(0,$i);
if($i != $j){
// swap items
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79888341/for-loop-counter-gives-error-for-array-index[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия