PHP 数组模糊查询
1 function search() { 2 $a=array( 3 '0' => array('id'=>1,'pid'=>0,'name'=>'水果'), 4 '1' => array('id'=>2,'pid'=>0,'name'=>'蔬菜'), 5 '2' => array('id'=>3,'pid'=>1,'name'=>'食品'), 6 '3' => array('id'=>4,'pid'=>2,'name'=>'运动'), 7 '4' => array('id'=>5,'pid'=>1,'name'=>'电脑'), 8 '5' => array('id'=>6,'pid'=>'香果','name'=>'香蕉'), 9 '6' => array('id'=>7,'pid'=>4,'name'=>'牛奶'), 10 '7' => array('id'=>8,'pid'=>5,'name'=>'西瓜'), 11 '8' => array('id'=>9,'pid'=>7,'name'=>'苹果'), 12 ); 13 $arr=$result=array(); 14 foreach ($a as $key => $value) { 15 16 foreach ($value as $valu) { 17 if(strstr($valu, '果') !== false) 18 { 19 array_push($arr, $key); 20 } 21 } 22 } 23 foreach ($arr as $key => $value) { 24 if(array_key_exists($value,$a)){ 25 array_push($result, $a[$value]); 26 } 27 } 28 return $result; 29 } 30 var_dump( search());
结果:
1 array(3) { 2 [0] => array(3) { 3 ["id"] => int(1) 4 ["pid"] => int(0) 5 ["name"] => string(6) "水果" 6 } 7 [1] => array(3) { 8 ["id"] => int(6) 9 ["pid"] => string(6) "香果" 10 ["name"] => string(6) "香蕉" 11 } 12 [2] => array(3) { 13 ["id"] => int(9) 14 ["pid"] => int(7) 15 ["name"] => string(6) "苹果" 16 } 17 }
人生得意须尽欢,莫使金樽空对月.