Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * If one or more params[] element exists in data[].
- * Our function will return the elements conserner
- */
- $params = [
- "nom" => 'titof',
- "prenom" => 'edoe',
- "age" => '25'
- ];
- $data = [
- 0 => [
- 'nom' => 'john',
- 'prenom' => 'doe',
- 'age' => '25'
- ],
- 1 => [
- 'nom' => 'lola',
- 'prenom' => 'la grandi',
- 'age' => '72'
- ],
- 2 => [
- 'nom' => 'michael',
- 'prenom' => 'Pamal',
- 'age' => '25'
- ]
- ];
- function getFilteredArray($params, $data) {
- $filtredArray = [];
- foreach($params as $key => $value) {
- foreach($data as $index => $item) {
- if(array_key_exists($key, $item) && in_array($value, $params)) {
- if($item[$key] == $value ){
- $filtredArray[$index] = $item;
- } else {
- continue;
- }
- }
- }
- }
- return $filtredArray;
- }
- var_dump(getFilteredArray($params, $data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement