php closure experiment

巴扎黑
Release: 2023-03-02 17:12:02
Original
1016 people have browsed it

class IArray extends ArrayObject  
{      
static function make($array)      
{          
return new self($array);      
}
function map($func)      
{          
$res = new self();
foreach ($this as $k => $v)   
{   
$res[$k] = $func($k, $v);          
}
return $res;      
}
function filter($func)      
{          
$res = new self();
foreach ($this as $k => $v)   
{              
if ($func($k, $v))   
{                  
$res[$k] = $v;             
}      
}
return $res;      
}
}
$nums = array(10,20,30,40);
$res = IArray::make($nums)->filter(function($k, $v) { return $v > 15; })->map(function($k, $v) { return $v * 2; });
print_r($res);
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!