В рамках более крупного проекта я получаю массивы в подобной форме:
$out = array(
'2011' => ['Out', 'arrv'],
'2012' => ['Out'],
'2013' => ['Out'],
'2014' => ['Out'],
'2015' => ['Out'],
'2016' => ['some', 'none', 'test'],
'2017' => ['Out'],
'2018' => ['Out'],
'2019' => ['Out'],
'2020' => ['Out', 'did'],
'2021' => ['Out'],
'2022' => ['Out'],
'2023' => ['Out', 'did']
);
Мне нужно удалить последовательные строки «Out», но оставить первую и последнюю. Моя неудачная попытка:
foreach ($out as $dtp=>$dto) {
if (count($dto) == 1) {
if (str_contains($dto[0], "Out")) {
$idx = array_search($dtp, array_keys($out));
echo "$dtp contains the string; index is $idx" . PHP_EOL;
var_dump($out[intval(array_keys($idx - 1))]);
if ((str_contains($out[$idx-1][0], "Out")) && (str_contains($out[$idx+1][0], "Out")) && count($out[$idx-1] == 1) && count($out[$idx+1] == 1)) {
array_splice($out, $idx, 1);
}
}
}
}
Счетчики существуют, поскольку на одну временную метку может приходиться несколько событий. Echo возвращает правильный индексный номер, но var_dump всегда возвращает NULL.
Текущие выходные данные отладки:
2017 contains the string; index is 6
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2018 contains the string; index is 7
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2019 contains the string; index is 8
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2021 contains the string; index is 10
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2022 contains the string; index is 11
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
Хотя я не предоставил полный исходный код, результат массива примеров будет таким:
[
'2011' => ['Out', 'arrv'],
'2012' => ['Out'],
'2015' => ['Out'],
'2016' => ['some', 'none', 'test'],
'2017' => ['Out'],
'2019' => ['Out'],
'2020' => ['Out', 'did'],
'2021' => ['Out'],
'2022' => ['Out'],
'2023' => ['Out', 'did']
]
Подробнее здесь: https://stackoverflow.com/questions/790 ... a-2d-array
Удалить последовательные дубликаты указанного значения из двумерного массива ⇐ Php
Кемеровские программисты php общаются здесь
1728370411
Anonymous
В рамках более крупного проекта я получаю массивы в подобной форме:
$out = array(
'2011' => ['Out', 'arrv'],
'2012' => ['Out'],
'2013' => ['Out'],
'2014' => ['Out'],
'2015' => ['Out'],
'2016' => ['some', 'none', 'test'],
'2017' => ['Out'],
'2018' => ['Out'],
'2019' => ['Out'],
'2020' => ['Out', 'did'],
'2021' => ['Out'],
'2022' => ['Out'],
'2023' => ['Out', 'did']
);
Мне нужно удалить последовательные строки «Out», но оставить первую и последнюю. Моя неудачная попытка:
foreach ($out as $dtp=>$dto) {
if (count($dto) == 1) {
if (str_contains($dto[0], "Out")) {
$idx = array_search($dtp, array_keys($out));
echo "$dtp contains the string; index is $idx" . PHP_EOL;
var_dump($out[intval(array_keys($idx - 1))]);
if ((str_contains($out[$idx-1][0], "Out")) && (str_contains($out[$idx+1][0], "Out")) && count($out[$idx-1] == 1) && count($out[$idx+1] == 1)) {
array_splice($out, $idx, 1);
}
}
}
}
Счетчики существуют, поскольку на одну временную метку может приходиться несколько событий. Echo возвращает правильный индексный номер, но var_dump всегда возвращает NULL.
Текущие выходные данные отладки:
2017 contains the string; index is 6
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2018 contains the string; index is 7
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2019 contains the string; index is 8
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2021 contains the string; index is 10
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
2022 contains the string; index is 11
PHP Warning: array_keys() expects parameter 1 to be array, integer given in /home/redacted/pub/dbg2.php on line 28
PHP Notice: Undefined offset: 0 in /home/redacted/pub/dbg2.php on line 28
NULL
Хотя я не предоставил полный исходный код, результат массива примеров будет таким:
[
'2011' => ['Out', 'arrv'],
'2012' => ['Out'],
'2015' => ['Out'],
'2016' => ['some', 'none', 'test'],
'2017' => ['Out'],
'2019' => ['Out'],
'2020' => ['Out', 'did'],
'2021' => ['Out'],
'2022' => ['Out'],
'2023' => ['Out', 'did']
]
Подробнее здесь: [url]https://stackoverflow.com/questions/79063926/remove-consecutive-duplicates-of-a-specified-value-from-a-2d-array[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия