Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] do.. while w php 8 ?, Dostosowuje skrypt do php 8 i jest problem z each()
starterrrrr
post 3.09.2021, 09:12:39
Post #1





Grupa: Zarejestrowani
Postów: 138
Pomógł: 0
Dołączył: 7.01.2015

Ostrzeżenie: (0%)
-----


Witam.

Dostosowuje skrypt do PHP 8.
Funkcja each() w php 8 jest już usunięta. Jak proponujecie przerobić poniższy kod na pasujący do php 8 ?

Pętla wykonuje kolejne iteracje w momencie gdy przekazana zmienna jest tablica.
  1.  
  2. do {
  3.  
  4. ...
  5.  
  6. } while (is_array($ilosc_lub_array) && list($klucz, $ilosc) = each($ilosc_lub_array));
  7.  


Ten post edytował starterrrrr 3.09.2021, 09:37:29
Go to the top of the page
+Quote Post
viking
post 3.09.2021, 15:55:22
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Zamień na foreach. Ten do/while też pewnie zbędny. Do tego od 7.1 masz https://www.php.net/manual/en/migration71.n...y-destructuring


--------------------
Go to the top of the page
+Quote Post
starterrrrr
post 6.09.2021, 09:08:41
Post #3





Grupa: Zarejestrowani
Postów: 138
Pomógł: 0
Dołączył: 7.01.2015

Ostrzeżenie: (0%)
-----


Analizuje ten przypadek i problemem jest to, że raz dostaje tablice a raz zmienną.

Jak jest zmienna to ma się wykonać raz a jak jest tablica to ma przelecieć po elementach. Jakiś pomysł ja kto rozwiązac?

Kod z pętli wstawić w funkcje i dać warunek, jeżeli tablica to wykonuje petle jak nie to leci wykonanie funkcji?
Go to the top of the page
+Quote Post
viking
post 6.09.2021, 09:34:12
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Z założenia metoda/funkcja powinna zwracać jednolity typ danych.


--------------------
Go to the top of the page
+Quote Post
nospor
post 6.09.2021, 10:35:58
Post #5





Grupa: Moderatorzy
Postów: 36 447
Pomógł: 6292
Dołączył: 27.12.2004




poprostu zrob tablice z tej zmiennej a ppotem foreach normalnie i po sprawie

  1. if (!is_array($ilosc_lub_array)) {
  2. $ilosc_lub_array = [$ilosc_lub_array];
  3. }
  4.  
  5. foreach ($ilosc_lub_array as $ilosc) {
  6. // tu twoj kod z while
  7. }
  8.  


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
starterrrrr
post 6.09.2021, 11:24:36
Post #6





Grupa: Zarejestrowani
Postów: 138
Pomógł: 0
Dołączył: 7.01.2015

Ostrzeżenie: (0%)
-----


Cytat(nospor @ 6.09.2021, 11:35:58 ) *
poprostu zrob tablice z tej zmiennej a ppotem foreach normalnie i po sprawie

  1. if (!is_array($ilosc_lub_array)) {
  2. $ilosc_lub_array = [$ilosc_lub_array];
  3. }
  4.  
  5. foreach ($ilosc_lub_array as $ilosc) {
  6. // tu twoj kod z while
  7. }
  8.  


Pierwszy raz widzę taki zapis ohmy.gif
$ilosc_lub_array = [$ilosc_lub_array]; smile.gif

Wieczorem sprawdzę.

Ten post edytował starterrrrr 6.09.2021, 11:25:05
Go to the top of the page
+Quote Post
nospor
post 6.09.2021, 11:39:23
Post #7





Grupa: Moderatorzy
Postów: 36 447
Pomógł: 6292
Dołączył: 27.12.2004




[cos] to skrocona wersja dla array(cos)

Mozesz nawet wywalic tego if od razu zrobic:
  1. $ilosc_lub_array = (array)$ilosc_lub_array; // tutaj rzutujesz na tablice. Jak cos jest nietablica, to bedzie. A jak cos jest tablica, to nic sie nie zmieni
  2.  
  3.  
  4. foreach ($ilosc_lub_array as $ilosc) {
  5. // tu twoj kod z while
  6. }


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.04.2024 - 05:11