PHP TP 获取二维数组根据条件取出想要的值
public function ArrList(){ //$arr是随机,现在我定义的是不随机的,后面取出数据库的也可以用 $arr = [ [ "id" => 1, "view" => 2, 'category_id' => 2, 'title' => "标题一", "model" => 12, 'desc' => "摘要", ], [ "id" => 123, "view" => 2, 'category_id' => 1, 'title' => "标题二", "model" => 101, 'desc' => "摘要", ], [ "id" => 3, "view" => 3, 'category_id' => 3, 'title' => "标题三", "model" => 101, 'desc' => "摘要", ], [ "id" => 4, "view" => 12, 'category_id' => 2, 'title' => "标题四", "model" => 3, 'desc' => "摘要", ], [ "id" => 5, "view" => 10, 'category_id' => 5, 'title' => "标题五", "model" => 101, 'desc' => "摘要", ], [ "id" => 5, "view" => 10, 'category_id' => 5, 'title' => "标题五", "model" => 9, 'desc' => "摘要", ], ]; // 现在我要取出model 为101的,而且数量是2个,这里为啥这样做,我也不知道,因为我弄的是连表查询出来的数据,要把这个模型,拿出来 $i = 0;//重新定义坐标为0 foreach ($arr as $k=>$v){ if($v['model'] == 101){ //取出前2条放在首页显示,其他的跟原有数据正常排序 $i++; if($i<=2){ $list['num'][] = $v; }else{ //这里要跟下面的list一样,不然数据就会没了,这是剩下的101模型的数据 $list['list'][] = $v; } }else{ $list['list'][] = $v; } } return $list; }