Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> array_filter z obiektami
shpaque
post 5.07.2018, 19:58:26
Post #1





Grupa: Zarejestrowani
Postów: 651
Pomógł: 3
Dołączył: 31.01.2011
Skąd: Warszawa

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


witam, siedze pol dnia, walcze i nie wiem gdzie jest blad, oto kod

  1. for ($i = $countCourses - 1; $i >= 0; $i--)
  2. {
  3. $courses[] = ['id' => $coursesDB -> course[$i]['id'], 'title' => $coursesDB -> course[$i]['title'], 'genre' => $coursesDB -> course[$i]['genre'], 'price' => $coursesDB -> course[$i]['price'], 'description' => $coursesDB -> course[$i] -> description, 'lessons' => count($coursesDB -> course[$i] -> lesson)];
  4. }
  5.  
  6. if (isset($_GET['cat']))
  7. {
  8. $category = $_GET['cat'];
  9.  
  10. if ($category === 'moje')
  11. {
  12. foreach ($userCourse as $uc)
  13. {
  14. $myCourseID[] = ['id' => $uc['id']];
  15. }
  16. $coursesFiltered = array_filter($courses, function ($course) use ($myCourseID) {return $course['id'] == $myCourseID;});
  17. print_r($coursesFiltered);
  18. }
  19. else
  20. {
  21. [php]$courses = array_filter($courses, function ($course) use ($category) {return $course['genre'] == $category;});

}
}[/php]

wszystko jest obiektami z xmla ale tablica $courses jest ok, opcja
  1. $courses = array_filter($courses, function ($course) use ($category) {return $course['genre'] == $category;});
dziala, ale jak mozna zrobic zeby filtrowal mi nie dosc ze obiekty a nie stringi to jeszcze z tablicy z wieloma zmiennymi zeby wyfiltrowal na podstawie drugiej tablicy ktora ma tylko id
Go to the top of the page
+Quote Post
KrzychuKa
post 6.07.2018, 09:30:45
Post #2





Grupa: Zarejestrowani
Postów: 5
Pomógł: 3
Dołączył: 25.06.2015

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


$myCourseID to array, więc nieda się porównać z 'id'.
  1. $myCourseID = array_column($userCourse, 'id');
  2.  
  3. $coursesFiltered = array_filter($courses, function ($course) use ($myCourseID) {
  4. return in_array($course['id'],$myCourseID);
  5. });


Go to the top of the page
+Quote Post
shpaque
post 6.07.2018, 13:18:17
Post #3





Grupa: Zarejestrowani
Postów: 651
Pomógł: 3
Dołączył: 31.01.2011
Skąd: Warszawa

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


ok, $myCourseID zwraca wowczas tablice:

  1. Array ( [0] => SimpleXMLElement Object ( [0] => 1 ) [1] => SimpleXMLElement Object ( [0] => a2093874 ) )


natomiast
  1. $coursesFiltered = array_filter($courses, function ($course) use ($myCourseID) {return in_array($course['id'], $myCourseID);});

zwraca Array()

@EDIT - udało się,

problem polegal na tym, że zmienne w tablicy $userCourse były ciągle obiektami, kiedy dodałem (string) przy budowaniu tamtej tablicy, to array_filter zaskoczył smile.gif Dziękuję

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: 23.04.2024 - 18:34