cache; // $cache->delete(Yii::$app->params['cacheKey']['characteristic']); $cacheData = $cache->get(Yii::$app->params['cacheKey']['characteristic']); if(!$cacheData) { $char = \common\models\Characteristic::find(); $char->andWhere(['del'=>1]); $char->andWhere(['is_show'=>1]); $data = $char->asArray()->all(); if(!empty($data)) { $cacheData = array_column($data,'name','id'); $cache->set(Yii::$app->params['cacheKey']['characteristic'],$cacheData); } } $resultData = []; if(is_array($id)) { $charData = []; foreach ($cacheData as $key=>$val) { if(in_array($key,$id)) { $charData[$key] = $val; } } if(!empty($charData) && count($charData)>$num) { $rand = array_rand($charData,3); $resultData[] = $cacheData[$rand[0]]; $resultData[] = $cacheData[$rand[1]]; $resultData[] = $cacheData[$rand[2]]; } else { $resultData = $charData; } } return $resultData; } }