PHP操作 二维数组模拟mysql函数
public function monimysqltest(){
$testarray=array(
array('ss'=>'1','dd'=>'11'),
array('ss'=>'2','dd'=>'22'),
array('ss'=>'3','dd'=>'33'),
array('ss'=>'4','dd'=>'44'),
);
$sa=$this->arrayfilter($testarray,'ss',array(3,4));
echo'模拟where<br>';
print_r($sa);
echo'<br>';
$array[] = array('id'=>1,'price'=>50);
$array[] = array('id'=>2,'price'=>70);
$array[] = array('id'=>3,'price'=>50);
$array[] = array('id'=>4,'price'=>20);
$arre=$this->arraysort($array,'price',SORT_DESC);
echo'模拟orderby<br>';
print_r($arre);
echo'</br>';$aa = array(
array('id' => 123, 'name' => '张三'),
array('id' => 123, 'name' => '李四'),
array('id' => 124, 'name' => '王五'),
array('id' => 125, 'name' => '赵六'),
array('id' => 126, 'name' => '赵六')
);
$key = 'id';
$bb=$this->assoc_unique($aa, $key);
echo'模拟groupby<br>';
print_r($bb);echo'模拟limit直接PHParray_slice这个函数搞定<br>';
}/*PHP模仿mysql where筛选*/
function arrayfilter($arr,$key,$val){
$list=array();
foreach ($arr as $k=>$v){
foreach ($val as $k1=>$v1){
if($v[$key]==$v1){
$list[]=$v;
}
}}
return $list;
}/*PHP二维排序排序*/
function arraysort($arr,$key1,$paixu){foreach ($arr as $key=>$value){
$price[$key] = $value[$key1];
}array_multisort($price,$paixu,$arr);
return $arr;
}//模拟groupby
function assoc_unique($arr, $key) {
$arr1=$arr;
$tmp_arr = array();
foreach ($arr as $k => $v) {
if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
unset($arr[$k]);
} else {
$tmp_arr[] = $v[$key];
}
}
foreach ($arr as $k=>$v){
$arr[$k]['lists']=$this->arrayfilter($arr1,$key,array($v[$key]));
}
return $arr;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19046.html