Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Filtrowanie i usówanie elementów tablicy
lulia
post 21.06.2017, 09:56:13
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 21.06.2017

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


Cześć. Próbuję poradzić sobie z przeszukaniem tablicy i usunięciu niechcianych elementów. Mam taka tablic:
  1. array:3 [
  2. 0 => array:2 [
  3. "internal_nr_id" => "200007"
  4. "status" => "S"
  5. ]
  6. 1 => array:2 [
  7. "internal_nr_id" => "200007"
  8. "status" => "E"
  9. ]
  10. 2 => array:2 [
  11. "internal_nr_id" => "200008"
  12. "status" => "S"
  13. ]
  14. ]

I chciałem zapisać do nowej tablicy wyłącznie internal_nr_id które mają tylko status S (w tym przypadku 200008).
Próbowałem to zrobić w taki sposób:
  1. for ($i = 0; $i < count($results2); $i++) {
  2.  
  3. if($results2[$i]['internal_nr_id'] === $results2[$i]['internal_nr_id'] && $results2[$i]['status'] === 'S')
  4. {
  5.  
  6.  
  7. $testowaS[$i]=$results2[$i]['internal_nr_id'];
  8. }
  9. }
  10.  
  11. for ($i = 0; $i < count($results2); $i++) {
  12.  
  13. if($results2[$i]['internal_nr_id'] === $results2[$i]['internal_nr_id'] && $results2[$i]['status'] === 'E')
  14. {
  15.  
  16.  
  17. $testowaE[$i]=$results2[$i]['internal_nr_id'];
  18. }
  19. }
  20. $inters = array_intersect($testowaE, $testowaS);

I od tego miejsca mam problem.Bo mając już internal nr który nie spełnia tych wymagań nie wiem jak wywalić go z tablicy.Próbowałem zrobić to za pomocą array_diff,ale wciąż sypie błedami (Notice: Array to string conversion).

Domyśłam się, że ten kod jest słaby,ale to jedyne co przyszło mi do głowy. Albo da się to zrobic inaczej?!?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
Pyton_000
post 21.06.2017, 10:05:35
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


  1. <?php
  2.  
  3. $table = [
  4. [
  5. "internal_nr_id" => "200007",
  6. "status" => "S",
  7. ],
  8. [
  9. "internal_nr_id" => "200007",
  10. "status" => "E",
  11. ],
  12. [
  13. "internal_nr_id" => "200008",
  14. "status" => "S",
  15. ],
  16. ];
  17.  
  18. $data = [];
  19. foreach($table as $val) {
  20. $data[$val['internal_nr_id']][] = $val['status'];
  21. }
  22.  
  23. $data = array_filter($data, function($elem) {
  24. return count($elem) == 1 && $elem[0] = 'S';
  25. });
  26.  
Go to the top of the page
+Quote Post
nospor
post 21.06.2017, 10:08:07
Post #3





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




I tak troche od tematu, ale oczy aż bolą

usuwanie


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

"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
lulia
post 21.06.2017, 10:21:01
Post #4





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 21.06.2017

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


Wielkie dzięki smile.gif
facepalmxd.gif , ortografia oneeyedsmiley02.png
Go to the top of the page
+Quote Post
woxala123
post 21.06.2017, 21:15:40
Post #5





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


nie ortografia tylko ortogravia
Go to the top of the page
+Quote Post
Tomplus
post 21.06.2017, 21:29:55
Post #6





Grupa: Zarejestrowani
Postów: 1 875
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


@lulia
Ale jak błąd zauważyłeś, to popraw go. Nie rób wstydu sobie bardziej tongue.gif
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: 19.07.2025 - 20:20