Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

php array of objects filter

//Array_filter to filter employee whose salaries are more than 12,000.
$filtered_arr = array_filter($employees_arr,function($employee){ return $employee->get_salary() > 12000;});
 
//Gets the filtered array.
print_r($filtered_arr);
 
 
/*
OUTPUT
Array
(
    [0] => Employee Object   
        (
            [id] => 1        
            [name] => Rachel 
            [salary] => 15000
        )
 
    [1] => Employee Object   
        (
            [id] => 2        
            [name] => Anna   
            [salary] => 14000
        )
 
    [2] => Employee Object
        (
            [id] => 3
            [name] => Robert
            [salary] => 13000
        )
 
)
*/
Source by fuelingphp.com #
 
PREVIOUS NEXT
Tagged: #php #array #objects #filter
ADD COMMENT
Topic
Name
7+5 =