Laravel 从集合中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41202887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:56:26  来源:igfitidea点击:

Laravel get values from collection

phplaravellaravel-5

提问by user947668

If collection is created from an array:

如果集合是从数组创建的:

$collection = collect();
$collection->push([
        'var1'=>'value1',
        'var2'=>'value2'
       ]);

is there possibility to get value for specific key similar way to eloquent collection attributes?

是否有可能以类似于雄辩的集合属性的方式获取特定键的值?

$collection->var1

回答by Ivakhnenko Aleksey

My variant:

我的变种:

$collection = collect();
$collection->push([
    'var1'=>'value1',
    'var2'=>'value2'
]);
$value = $collection->get('var1'); // value1

https://laravel.com/docs/5.6/collections#method-get

https://laravel.com/docs/5.6/collections#method-get

回答by prateekkathal

No, but you can definitely do something like

不,但你绝对可以做类似的事情

$collection->first(function($value, $key) {
  return $key == 'var1';
});

or simply what @AlexeyMezenin suggested.

或者只是@AlexeyMezenin 建议的内容。

$collection[0]['var1'];

回答by Birdy

My personal touch though I am still learning the basics myself so don't shoot me down for trying! I know its a little extra code but from experience you can get caught out with offset exception errors when calling $collection[0]explicitly (Hope I am making sense as this has been a personal gotcha experience)

虽然我自己仍在学习基础知识,但我的个人风格所以不要因为我的尝试而失望!我知道它有一些额外的代码,但是根据经验,在$collection[0]显式调用时,您可能会遇到偏移异常错误(希望我说得通,因为这是个人问题)

My approach would be the following

我的方法如下

$collection = collect();
$collection->push([
    'var1'=>'value1',
    'var2'=>'value2'
]);

for ($x = 0; $x < count($collection); $x++) {
    if (isset($collection[$x])) {
        $var = $collection[$x];
    }
}

// Now call what ever variable from the collection you wish:

echo $var['var1'];
echo $var['var2'];

Hope that helps, Like I said though I am far from experienced with php/laravel and I am still learning from research myself :)

希望能有所帮助,就像我说的,虽然我对 php/laravel 的经验还很远,但我自己仍在从研究中学习:)

回答by DevOverlord

To set a key'd value in a collection you can use the put()method.

要在集合中设置键的值,您可以使用该put()方法。

$collection = collect();

$collection->put("var1", "value1");
$collection->put("var2", "value2");
$collection->put("var3", "value3");

To push just a value without a key to the collection, use the push()method, like you did in the OP.

要将没有键的值推送到集合,请使用该push()方法,就像您在 OP 中所做的那样。

$collection->push("value4");

To retrieve those value, use get()method;

要检索这些值,请使用get()方法;

$collection->get("var2");

Want to see the contents of the collection? Use the dd()method on the collections instances. Both of the following work.

想看合集的内容吗?dd()在集合实例上使用该方法。以下两项工作。

dd($collection)

or

或者

$collection->dd()