Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: tolik8 user
Added: Jul 7, 2018 12:10 PM
Views: 7
Tags: array_filter
array_filter example
  1. <?php
  2.  
  3. $users = [
  4.     [
  5.         "name" => "John",
  6.         "surname" => "Doe",
  7.         "age" => 20,
  8.         "email" => "john@example.com"
  9.     ],
  10.    
  11.     [
  12.         "name" => "Jane",
  13.         "surname" => "Doe",
  14.         "age" => 18,
  15.         "email" => "jane@example.com"
  16.     ],
  17.    
  18.     [
  19.         "name" => "Mary",
  20.         "surname" => "Doe",
  21.         "age" => 22,
  22.         "email" => "mary@example.com"
  23.     ],
  24.    
  25. ];
  26.  
  27. var_dump($users);
  28.  
  29. $func = function($item) {
  30.     if ($item["age"] > 18) {
  31.         return $item;
  32.     }
  33. };
  34.  
  35. $users2 = array_filter($users, $func);
  36.  
  37. var_dump($users2);
  38.