Filter multidimensional arrays using unset and array_filter

WBOY
Release: 2016-08-08 09:27:23
Original
1642 people have browsed it
class A{
	/**
	 *  多维数组过滤包含指定元素的子数组
	 *	 $menu_list				菜单(一个四维数组)
	 *  $mmenu 					接收过滤后的菜单
	 *  $member_limits  	保存在表中的菜单字段
	 */
	public function Multi_dimensional_array (){
		//    
		$menu_list = $this->_getMemberMenuList();
		$mmenu = array();
		$member_limits = $this->member_limits();
		// 用&对原始数据进行写操作
		foreach($menu_list as  &$value){
			foreach($value['child'] as $k => $v){
				if(!in_array($k,$member_limits)){
					//清除元素
					unset($value['child'][$k]);
				}
			}	
		}
		// array($this,"_Filter") 相当于 $this->_Filter()
		$mmenu = array_filter($menu_list, array($this,"_Filter"));
	}
	
	/**
	 *  array_filter 数组回调函数
	 *  将判断为空的指定元素过滤掉
	 */
	protected function _Filter($value){
		foreach($value['child'] as $k => $v){
			$result = isset($value['child']);
			if($result){
				return $result ;
			}
		}
	}
}
Copy after login

The above introduces the use of unset and array_filter to filter multi-dimensional arrays, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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!