/** * @param array $tree * @param array $options * @return array */ public static function menu(array $tree = [], array $options = []) { $array = []; $attributes = !empty($options['attributes']) ? $options['attributes'] : ['icon' => 'fa fa-circle-o']; $lkey = !empty($options['lkey']) ? $options['lkey'] : 'lft'; $levelkey = !empty($options['level']) ? $options['level'] : 'lvl'; $label = !empty($options['label']) ? $options['label'] : 'title'; $level = 0; $i = 0; $_path = $i; $delimiter = '.'; foreach ($tree as $child) { $menu = ['label' => $child->{$label}, 'url' => [$child->url()], 'level' => $child->{$levelkey}]; if ($child->{$levelkey} > $level) { $i = 0; $_path = $_path . $delimiter . 'items'; } if ($child->{$levelkey} > 0) { $path = $_path . $delimiter . $i; } else { $path = $i; } ArrayHelper::setValue($array, $path, $menu, $delimiter); $level = $child->{$levelkey}; $i++; } return $array; }
public function array_to_json_string(array $array, $tabs = "\t") { $json_str = "{\n"; $json_chunks = []; foreach ($array as $k => $el) { if (is_array($el)) { if (!ArrayHelper::is_assoc_array($el)) { $el = array_map(function ($value) use($tabs) { if ($value === true) { return '"1"'; } elseif ($value === false) { return '""'; } elseif (is_array($value)) { return $this->array_to_json_string($value, $tabs . "\t"); } elseif (is_object($value)) { return $this->array_to_json_string((array) $value, $tabs . "\t"); } else { return "\"{$value}\""; } }, $el); $json_chunks[] = "{$tabs}\"{$k}\"\t: [" . implode(',', $el) . ']'; } else { $json_chunks[] = "{$tabs}\"{$k}\"\t: " . $this->array_to_json_string($el, $tabs . "\t"); } } elseif (is_callable($el)) { continue; } elseif (is_object($el)) { $json_chunks[] = "{$tabs}\"{$k}\"\t: " . $this->array_to_json_string((array) $el, $tabs . "\t"); } elseif (is_resource($el)) { continue; } else { $json_chunks[] = "{$tabs}\"{$k}\"\t: \"{$el}\""; } } $json_str .= implode(",\n", $json_chunks); $json_str .= "\n" . preg_replace("/\t/", '', $tabs, 1) . '}'; return $json_str; }
public function getTree($pid = 0, $fields = []) { $categorys = self::$model->find()->where(['type' => Meta::TYPE_ARTICLE_CATEGORIES, 'status' => Meta::STATUS_ACTIVE])->select($fields)->asArray()->all(); return ArrayHelper::listToTree($categorys); }
/** * @covers common\adapters\MysqlAdapter::get_configuration */ public function test_get_configuration() { self::assertTrue(ArrayHelper::is_assoc_array($this->model->get_configuration())); }
/** * 获取演员评论信息 * @param $actor_id * @param $index * @param $count * @return array|int|\yii\mongodb\ActiveRecord */ public function commentList($actor_id, $index, $count) { if (!is_numeric($actor_id)) { return ErrorConstant::PARAM_ERROR; } $index = is_numeric($index) ? $index : self::PAGE_INDEX_DEFAULT; $count = is_numeric($count) ? $count : self::PAGE_COUNT_DEFAULT; $offset = ($index - 1) * $count; //判断演员是否存在 $actor = ActorModel::find()->where(['status' => ActorModel::ACTOR_STATUS_OK, 'id' => $actor_id])->asArray()->one(); if ($actor === []) { return ErrorConstant::ACTOR_NOT_EXISTS; } //获取评论列表 $commentList = ActorCommentModel::find()->where(['actor_id' => (int) $actor_id])->offset($offset)->limit($count)->asArray()->orderBy('create_time DESC')->all(); if ($commentList === []) { return $commentList; } $user_id = array_unique(array_merge(ArrayHelper::getColumn($commentList, 'user_id'), ArrayHelper::getColumn($commentList, 'target_user_id'))); //获取用户信息 $comment_user = UserModel::find()->where(['in', 'id', $user_id])->asArray()->all(); $comment_user = ArrayHelper::index($comment_user, 'id'); //获取用户评论的评论信息 $comment_id = array_unique(ArrayHelper::getColumn($commentList, 'target_comment_id')); $target_comment = ActorCommentModel::find()->where(['in', '_id', $comment_id])->asArray()->all(); $target_comment = ArrayHelper::index($target_comment, '_id'); foreach ($commentList as $key => $comment) { $commentList[$key]['user_name'] = $comment_user[$comment['user_id']]['nick_name']; $commentList[$key]['icon_url'] = $comment_user[$comment['user_id']]['icon_url']; if ($comment['target_comment_id'] >= 0) { $commentList[$key]['target_user_name'] = $comment_user[$comment['target_user_id']]['nick_name']; $commentList[$key]['target_user_icon_url'] = $comment_user[$comment['target_user_id']]['icon_url']; $commentList[$key]['target_comment_content'] = $target_comment[$comment['target_user_id']]['content']; } } return $commentList; }
/** * @expectedException InvalidArgumentException * @covers common\helpers\ArrayHelper::is_assoc_array */ public function test_is_assoc_array_with_no_array() { ArrayHelper::is_assoc_array(1); }