我确定之前已经问过这个问题,我很抱歉没先找到它.
原始数组:
[0] => Array
(
[categoryId] => 1
[eventId] => 2
[eventName] => 3
[vendorName] => 4
)
[1] => Array
(
[categoryId] => 5
[eventId] => 6
[eventName] => 7
[vendorName] => 8
)
[2] => Array
(
[categoryId] => 9
[eventId] => 10
[eventName] => 11
[vendorName] => 12
)
Run Code Online (Sandbox Code Playgroud)
我希望得到的结果是:print_r(get_values_from_a_key_in_arrays('categoryId',$ array));
[0] => 1
[1] => 5
[2] => 9
Run Code Online (Sandbox Code Playgroud)
我只是在寻找比编写自己的基于foreach的功能更清晰的东西.如果foreach是答案,我已经有了.
编辑:我不想使用硬编码密钥,我只是展示了一个示例调用解决方案.谢谢!^ _ ^
PHP 5.3快速抓取解决方案:
private function pluck($key, $data) {
return array_reduce($data, function($result, $array) …
Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel.5.3以下是我的查询
$ProjectManagers = Employees::where("designation" , 1)
->pluck(DB::raw('CONCAT(first_name," ",last_name) AS name'),'id');
Run Code Online (Sandbox Code Playgroud)
这引发了一个错误
isset或empty中的非法偏移类型
我可以知道这是否是正确的方法?
如果我不使用联系和使用像
$ProjectManagers = Employees::where("designation" , 1)->pluck('first_name','id');
Run Code Online (Sandbox Code Playgroud)
哪个工作正确并给我结果
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[8] => Punit
)
)
Run Code Online (Sandbox Code Playgroud)
预期结果 :
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[8] => Punit Gajjar
)
)
Run Code Online (Sandbox Code Playgroud)
其中第一个名称和姓氏连接在一起.
我正在尝试从数据库中检索数据并将它们绑定到html选择标记,并绑定它们我需要使用pluck所以我得到我希望在数组中显示的字段(key => value),因为FORM: :选择.正常的采摘获得所有结果,而我想使用不同的.我的模特是房间,它看起来像:
class Room extends Eloquent
{
public $timestamps = false;
protected $casts = [
'price' => 'float',
'floor' => 'int',
'size' => 'float'
];
protected $fillable = [
'capacity',
'description',
'price',
'floor',
'size',
'type',
'photo_name'
];
}
Run Code Online (Sandbox Code Playgroud)
虽然我在控制器中使用的功能看起来像:
public function getRooms()
{
$roomType = Room::pluck('type','type');
$roomFloor = Room::pluck('floor','floor');
return view('roomgrid')->with('type',$roomType)->with('floor',$roomFloor);
}
Run Code Online (Sandbox Code Playgroud)
我的视图包含这段代码来获取楼层:
{{FORM::select('floor', $floor, null,['class'=>'basic'])}}
Run Code Online (Sandbox Code Playgroud)
像这样我得到重复的地板,我不想要.有什么办法让我可以得到不同的地板并采摘它们吗?提前致谢.
从数据库中提取时,我得到id
了字符串.
$alphabets = new Alphabet();
return $alphabets->pluck('name', 'id');
Run Code Online (Sandbox Code Playgroud)
产量
{
"1": "Apple",
"2": "Ball",
"3": "Cat"
}
Run Code Online (Sandbox Code Playgroud)
预期
{
1: "Apple",
2: "Ball",
3: "Cat"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我扭转ID
和name
,
return $alphabets->pluck('id', 'name');
Run Code Online (Sandbox Code Playgroud)
我得到id为整数.
{
"Apple": 1,
"Ball": 2,
"Cat": 3
}
Run Code Online (Sandbox Code Playgroud)
我不确定幕后发生了什么.但是我如何获得整数ID?实际上,旧的flash会话因为1 vs "1"
Form Collective 而没有设置值.
{!! Form::select('alphabet', $alphabets, null, ['class' => 'form-control', 'multiple' => true]) !!}
Run Code Online (Sandbox Code Playgroud) 我发现 Laravel 的pluck()
方法返回一个普通数组并select()
返回一个对象。
谁能解释一下这两种方法之间是否还有其他差异?
我有以下令人讨厌的嵌套列表
编辑:更新以包括value_I_dont_want
mylist <- list(
list(
nested_1 = list(
nested_2 = list(
list( value_I_want = "a", value_I_dont_want = "f"),
list( value_I_want = "b", value_I_dont_want = "g")
)
)
),
list(
nested_1 = list(
nested_2 = list(
list( value_I_want = "c", value_I_dont_want = "h"),
list( value_I_want = "d", value_I_dont_want = "i"),
list( value_I_want = "e", value_I_dont_want = "j")
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我想得到所有value_I_want
的
我知道我可以在 for 循环中使用以下代码
mylist[[x]]$nested_1$nested_2[[y]]$value_I_want
Run Code Online (Sandbox Code Playgroud)
但我想提高我的地图技能。map_chr
我了解当列表是单个级别时如何使用,但我没有找到很多关于从非常嵌套的列表中提取的资源。我也知道我可以使用,[[
但还没有找到合适的文档?
任何帮助表示赞赏!
我有一个庞大的表Foo,我需要从中提取特定字段Foo.who中的所有值。
该数组具有数百万行,但列中只有几千个不同的值who
。
如果桌子较小,我当然会用 Foo.pluck(:who)
如果我使用的Foo.find_in_batches do |a_batch|
每个集合都是Foo记录的数组,而不是Foo记录的activerecord集合,那么我不能使用.pluck()
AFAIK和AFAIK提取who
列的唯一方法是通过.map(&:who)
对数组进行迭代的方式。
有没有一种方法可以who
批量从Foo中拔出色谱柱,而无需在每个批次的每个元素上进行迭代以提取who
色谱柱?
我知道Laravel已删除lists()
函数并将函数签名移为的事实pluck()
。但是,对于想要从Laravel 4.x升级到Laravel 5.4的人来说,这会导致很多工作。
因此,我试图找到一种方法来仅利用现有函数(即lists()
在我的代码中)并且仅利用pluck()->toArray()
何时调用此函数。
class BaseModel extends Illuminate\Database\Query\Builder
public function __call($method, $args)
{
return call_user_func_array($this->method,$args);
}
public function lists($column){
return $this->pluck($column)->toArray();
}
Run Code Online (Sandbox Code Playgroud)
不工作!
原因:这需要与BaseModel类一起扩展。但是,它已经扩展了口才模型类。
尝试使用trait
like 添加Required函数
listingWorkAround.php
<?php
trait listsWorkAround
{
function lists($column){
return $this->pluck($column)->toArray();
}
}
Run Code Online (Sandbox Code Playgroud)
模型.php
<?php
namespace App;
use Watson\Rememberable\Rememberable;
use Illuminate\Database\Eloquent\Model as Eloquent;
abstract class Model extends Eloquent
{
use listsWorkAround;
use Rememberable;
}
Run Code Online (Sandbox Code Playgroud)
不,不是没有成功。
试图添加一个as ServiceProvider …
在我的侧边栏中,我显示了新创建的用户配置文件.个人资料belongs_to
用户和用户has_one_profile
.我意识到我只使用配置文件表中的3列,所以最好使用pluck.我也有link_to user_path(profile.user)
部分,所以我不得不告诉用户是谁.目前我正在使用includes
,但我不需要整个用户表.所以我使用了来自用户和配置文件表的许多列.
如何用拔毛来优化它?我尝试了几个版本,但总是遇到一些错误(大部分时间都没有定义profile.user).
我目前的代码:
def set_sidebar_users
@profiles_sidebar = Profile.order(created_at: :desc).includes(:user).limit(3) if user_signed_in?
end
create_table "profiles", force: :cascade do |t|
t.integer "user_id", null: false
t.string "first_name", null: false
t.string "last_name", null: false
t.string "company", null: false
t.string "job_title", null: false
t.string "phone_number"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar"
t.string "location"
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试提取嵌套列表的最后一个元素。我怎样才能做到这一点purrr
?在下面的示例中,结果应该是所有值为“c”的元素。我已经看到了这个,但我对 purrr 的解决方案特别感兴趣。
非常感谢
x <- list(list(1,2,"c"), list(3,"c"), list("c"))
x
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#>
#> [[1]][[2]]
#> [1] 2
#>
#> [[1]][[3]]
#> [1] "c"
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] 3
#>
#> [[2]][[2]]
#> [1] "c"
#>
#>
#> [[3]]
#> [[3]][[1]]
#> [1] "c"
map(x, purrr::pluck, tail)
#> Error in map(x, purrr::pluck, tail): could not find function "map"
map(x, purrr::pluck, length(x))
#> Error in map(x, purrr::pluck, length(x)): …
Run Code Online (Sandbox Code Playgroud)