本文整理汇总了PHP中Arr::pluck方法的典型用法代码示例。如果您正苦于以下问题:PHP Arr::pluck方法的具体用法?PHP Arr::pluck怎么用?PHP Arr::pluck使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Arr
的用法示例。
在下文中一共展示了Arr::pluck方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute_create
protected function execute_create(InputInterface $input, OutputInterface $output)
{
$client = $input->getOption('client');
if (!$client) {
// We can't use the generic `get_client()` for **creation**,
// because we need to verify that the user does **not** exist.
$clients = Arr::pluck(Ushahidi_Console_OAuth_Client::db_list(), 'client_id');
$ask = function ($client) use($clients) {
if (in_array($client, $clients)) {
throw new RuntimeException('Client "' . $client . '" already exists, try another name');
}
return $client;
};
$client = $this->getHelperSet()->get('dialog')->askAndValidate($output, 'Enter name of new client: ', $ask, FALSE);
}
$secret = $input->getOption('secret');
$redirect = $input->getOption('redirect');
if (!$secret) {
$secret = Text::random('distinct', 24);
}
if (!$redirect) {
$redirect = '/';
}
static::db_create(['client_id' => $client, 'client_secret' => $secret, 'redirect_uri' => $redirect]);
$input->setOption('client', $client);
return $this->execute_list($input, $output);
}
示例2: clear_all
public function clear_all()
{
$tables = array_diff(Arr::pluck($this->pdo->query('SHOW TABLES'), '0'), array(Migration_Driver_Mysql_Versions::SCHEMA_TABLE));
if (count($tables)) {
$this->execute("DROP TABLE " . join(', ', $tables));
}
return $this;
}
示例3: getTypeaheadList
public static function getTypeaheadList($col)
{
if (Auth::check() === false) {
return json_encode(array());
}
$user_id = Auth::get_user_id();
$res = DB::select($col)->from('posts')->where('user_id', '=', $user_id[1])->group_by($col)->execute()->as_array();
return json_encode(\Arr::pluck($res, $col));
}
示例4: get_enum_options
public static function get_enum_options($enum)
{
$options = static::query()->related('default')->related('items')->related('items.meta')->where('slug', $enum)->get_one();
if (is_null($options)) {
$options = array();
} else {
$options = $options->to_array();
$options = \Arr::pluck($options['items'], 'name', 'item_id');
}
return $options;
}
示例5: render_answer_partial
public function render_answer_partial()
{
$view = View::factory('question/matching/partial_view')->bind('lefts', $lefts)->bind('rights', $rights);
$attributes = $this->_orm !== null ? $this->_orm->attributes_as_array() : array();
$attribute = $attributes[0];
$pairs = unserialize($attribute['attribute_value']);
$lefts = Arr::pluck($pairs, 0);
$rights = Arr::pluck($pairs, 1);
shuffle($rights);
// var_dump($lefts, $rights, $pairs); exit;
return $view->render();
}
示例6: choice_type
/**
* Method to find out the possible correct answers
* for the question
* @param Array $attributes
* @return String unique|multiple
*/
public function choice_type($attributes)
{
$correct_status = Arr::pluck($attributes, 'correctness');
$only_correct = array_filter($correct_status);
$num_correct = count($only_correct);
if ($num_correct > 1) {
return 'multiple';
} else {
if ($num_correct == 1) {
return 'unique';
} else {
throw new Exception('Set of answers without atleast 1 correct answer');
}
}
}
示例7: getClient
protected function getClient(InputInterface $input, OutputInterface $output = NULL)
{
$client = $input->getOption('client');
if (!$client and $output) {
// If no client was given, and `$output` is passed, we can ask for
// the user interactively and validate it against the known clients.
$clients = Arr::pluck(self::db_list(), 'id');
$ask = function ($client) use($clients) {
if (!in_array($client, $clients)) {
throw new RuntimeException('Unknown client "' . $client . '", valid options are: ' . implode(', ', $clients));
}
return $client;
};
$client = $this->getHelperSet()->get('dialog')->askAndValidate($output, 'For which client? ', $ask, FALSE, NULL, $clients);
}
return $client;
}
示例8: action_read
/**
* Returns all models as JSON
*/
public function action_read()
{
try {
$id = $this->request->param('id', Arr::get($this->_input, 'id'));
//Kohana::$log->add(Log::DEBUG,$this->chart->as_array());
if (empty($id)) {
$data = $this->chart->read_all();
} else {
$data = $this->chart->as_array();
if (!is_null($this->time)) {
$url = sprintf('chart/series/%s/%s', $id, date('Y/m/d', $this->time));
$request = Request::factory($url)->execute();
$series = json_decode($request->body(), true);
Arr::set_path($data, "options.series", Arr::pluck($series, 'options'));
Arr::set_path($data, "options.subtitle.text", utf8_encode(strftime("%#d. %B %Y", $this->time)));
}
}
$this->response->body(json_encode($data));
} catch (Kohana_Exception $e) {
// Return HTTP 400: Bad Request
$this->response->status(400);
}
}
示例9: questions_attempted
/**
* Combined Setter/Getter method for questions_attempted array
* @param Array $ques (optional (default null))
* As a sideeffect, it will also set the $_questions_answers and
* $_questions_results arrays
*/
public function questions_attempted($ques = null)
{
if ($ques != null) {
$this->_questions_attempted = $ques;
$question_ids = array_keys($this->_questions_attempted);
$answers = Arr::pluck($this->_questions_attempted, 'answer');
$results = Arr::pluck($this->_questions_attempted, 'result');
$this->_questions_answers = array_combine($question_ids, $answers);
$this->_questions_results = array_combine($question_ids, $results);
return null;
}
return $this->_questions_attempted;
}
示例10: array_pluck
/**
* Pluck an array of values from an array.
*
* @param array $array
* @param string $value
* @param string $key
* @return array
*/
function array_pluck($array, $value, $key = null)
{
return Arr::pluck($array, $value, $key);
}
示例11: test_as_array
public function test_as_array()
{
// Test with no keys or values
$array = $this->collection->as_array();
$this->assertInternalType('array', $array);
foreach ($array as $offset => $model) {
$this->assertInstanceOf('Jam_Model', $model);
$this->assertEquals($this->data[$offset], $model->as_array());
}
// Test with keys
$array = $this->collection->as_array('name');
$this->assertInternalType('array', $array);
$offset = 0;
foreach ($array as $name => $model) {
$this->assertInstanceOf('Jam_Model', $model);
$this->assertEquals($this->data[$offset]['name'], $name);
$this->assertEquals($this->data[$offset], $model->as_array());
$offset += 1;
}
// Test with keys as meta aliases
$array = $this->collection->as_array(':primary_key');
$this->assertInternalType('array', $array);
$offset = 0;
foreach ($array as $name => $model) {
$this->assertInstanceOf('Jam_Model', $model);
$this->assertEquals($this->data[$offset]['id'], $name);
$this->assertEquals($this->data[$offset], $model->as_array());
$offset += 1;
}
// Test with values
$array = $this->collection->as_array(NULL, 'name');
$this->assertEquals(Arr::pluck($this->data, 'name'), $array);
// Test with values as aliases
$array = $this->collection->as_array(NULL, ':primary_key');
$this->assertEquals(Arr::pluck($this->data, 'id'), $array);
// Test with keys and values as aliases
$array = $this->collection->as_array(':name_key', ':primary_key');
$this->assertEquals(array_combine(Arr::pluck($this->data, 'name'), Arr::pluck($this->data, 'id')), $array);
}
示例12: has_math
/**
* Method to check if the question contains math expressions
* so that Mathjax typesetting can be triggered for the new dom tree
*/
public function has_math()
{
$pattern = '/\\$\\$.+\\$\\$/';
$to_check = array($this->question, $this->extra);
$attrs = Arr::pluck($this->attributes_as_array(), 'attribute_value');
$to_check = array_merge($to_check, $attrs);
foreach ($to_check as $i) {
$match = preg_match($pattern, $i);
if ($match) {
return true;
}
}
return false;
}
示例13: test_pluck_with_index
/**
* Test Arr::pluck()
*
* @test
* @dataProvider collection_provider
*/
public function test_pluck_with_index($collection)
{
$output = \Arr::pluck($collection, 'name', 'id');
$expected = array(2 => 'Bill', 5 => 'Chris', 7 => 'Bert');
$this->assertEquals($expected, $output);
}
示例14: report
/**
* Find and format a bunch of logs between two dates
* @return array The log data, ready for conversion into CSV or something similar
*/
public static function report($start_date = null, $end_date = null, $filters = array(), $group_by = null, $callback = null)
{
$qb = \CMF\Model\Log::findBy($filters);
$has_filters = count($filters) > 0;
// Filter from start date
if ($start_date !== null) {
if (is_string($start_date)) {
$start_date = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $start_date)));
$start_date = \DateTime::createFromFormat('Y-m-d H:i:s', $start_date);
}
if ($has_filters) {
$qb->andWhere("item.date >= ?1")->setParameter(1, $start_date);
} else {
$qb->where("item.date >= ?1")->setParameter(1, $start_date);
}
$has_filters = true;
}
// Filter to end date
if ($end_date !== null) {
if (is_string($end_date)) {
$end_date = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $end_date)));
$end_date = \DateTime::createFromFormat('Y-m-d H:i:s', $end_date);
}
if ($has_filters) {
$qb->andWhere("item.date < ?2")->setParameter(2, $end_date);
} else {
$qb->where("item.date < ?2")->setParameter(2, $end_date);
}
}
// Get the log entries
$entries = $qb->orderBy('item.date', 'ASC')->getQuery()->getArrayResult();
$types = array_values(array_unique(array_merge(array_unique(\Arr::pluck($entries, 'item_type')), array_unique(\Arr::pluck($entries, 'user_type')))));
$actions = array_values(array_unique(\Arr::pluck($entries, 'action')));
$types_ids = array();
$output = array();
// Normalize the group_by parameter
if ($group_by == 'user') {
$group_by = 'user_id';
} else {
if ($group_by == 'item') {
$group_by = 'item_id';
}
}
// Find the ids of the related users and items
foreach ($entries as $entry) {
// The user
if ($entry['has_user'] = isset($entry['user_type']) && isset($entry['user_id'])) {
$type = $entry['user_type'];
$ids = isset($types_ids[$type]) ? $types_ids[$type] : array();
if (!in_array($entry['user_id'], $ids)) {
$ids[] = $entry['user_id'];
$types_ids[$type] = $ids;
}
}
// The item
if ($entry['has_item'] = isset($entry['item_type']) && isset($entry['item_id'])) {
$type = $entry['item_type'];
$ids = isset($types_ids[$type]) ? $types_ids[$type] : array();
if (!in_array($entry['item_id'], $ids)) {
$ids[] = $entry['item_id'];
$types_ids[$type] = $ids;
}
}
// Add to the stats for this group if we're grouping
if ($group_by !== null) {
$group_value = $entry['item_type'] . '_' . $entry[$group_by];
if (!isset($output[$group_value])) {
$group = $entry;
foreach ($actions as $action) {
$group[$action . '_count'] = 0;
}
} else {
$group = $output[$group_value];
}
$group[$entry['action'] . '_count'] += 1;
$group['last_' . $action] = $entry['date']->format('d/m/Y H:i:s');
$output[$group_value] = $group;
} else {
$output[] = $entry;
}
}
// Now construct queries for each of the types, so we can grab them all in one swipe
foreach ($types as $type) {
if (!isset($types_ids[$type]) || count($types_ids[$type]) === 0) {
continue;
}
$ids = $types_ids[$type];
$types_ids[$type] = $type::select('item', 'item', 'item.id')->where('item.id IN(?1)')->setParameter(1, $ids)->getQuery()->getResult();
}
// Put the items into the log entries
foreach ($output as &$entry) {
// The user
if ($entry['has_user']) {
$user_type = $entry['user_type'];
$entry['user'] = \Arr::get($types_ids, $user_type . '.' . $entry['user_id'], null);
$entry['user_type_label'] = $user_type::singular();
//.........这里部分代码省略.........
示例15: test_pluck
/**
* Tests Arr::pluck()
*
* @test
* @dataProvider provider_pluck
* @param array $array
* @param string $key
* @param array $expected
*/
public function test_pluck(array $array, $key, $expected)
{
$array = Arr::pluck($array, $key);
$this->assertSame(count($expected), count($array));
$this->assertSame($expected, $array);
}