Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * If one or more params[] element exists in data[].
  5. * Our function will return the elements conserner
  6. */
  7.  
  8. $params = [
  9. "nom" => 'titof',
  10. "prenom" => 'edoe',
  11. "age" => '25'
  12. ];
  13.  
  14. $data = [
  15. 0 => [
  16. 'nom' => 'john',
  17. 'prenom' => 'doe',
  18. 'age' => '25'
  19. ],
  20. 1 => [
  21. 'nom' => 'lola',
  22. 'prenom' => 'la grandi',
  23. 'age' => '72'
  24. ],
  25. 2 => [
  26. 'nom' => 'michael',
  27. 'prenom' => 'Pamal',
  28. 'age' => '25'
  29. ]
  30. ];
  31.  
  32. function getFilteredArray($params, $data) {
  33. $filtredArray = [];
  34. foreach($params as $key => $value) {
  35. foreach($data as $index => $item) {
  36. if(array_key_exists($key, $item) && in_array($value, $params)) {
  37. if($item[$key] == $value ){
  38. $filtredArray[$index] = $item;
  39. } else {
  40. continue;
  41. }
  42. }
  43. }
  44. }
  45. return $filtredArray;
  46. }
  47.  
  48.  
  49. var_dump(getFilteredArray($params, $data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement