Laravel 集合方法 has() 和 contains() 之间的区别
Difference between Laravel Collection methods has() and contains()
我无法理解 Laravel 收集方法 has()
和 contains()
之间的区别。
The contains() method takes a single value, a key-value pair of parameters or a callback function and returns a boolean value of the value is present in the collection or not.
所以基本上,它 returns 一个基于值存在的布尔值。
has() - returns a boolean value if a key value is present in a collection or not.
这也是returns一个基于值存在的布尔值?
不知何故我不明白它们的区别。
希望有人能给我解释一下或分享一些有用的链接,我将不胜感激。
has
用于键,contains
用于值。
$collection = collect(['name' => 'Desk', 'price' => 100]);
$collection->has('name'); // true
$collection->has('Desk'); // false
$collection->contains('name'); // false
$collection->contains('Desk'); // true
你好,我认为不同之处在于 has() 方法仅搜索键,例如:
$collection = collect(['account_id' => 1, 'product' => 'Desk', 'amount' => 5]);
$collection->has('product');
// true
$collection->has(['product', 'amount']);
// true
$collection->has(['amount', 'price']);
// false
and contains 方法() 用于确定集合中是否存在给定的$key。此外,可以指定一个可选的 $value,用于检查给定的 key/value 对是否存在于集合中。
示例 1:最基本的用法:
use Illuminate\Support\Collection;
// Create a new collection instance.
$collection = new Collection([
'bear',
'whale',
'chicken',
'tardigrade'
]);
// true
$collection->contains('bear');
// true
$collection->contains('tardigrade');
// false
$collection->contains('dog');
示例 2:使用 contains 方法检查给定的 key/value 对是否存在于包含数组作为项目的集合中:
use Illuminate\Support\Collection;
// Create a new collection instance.
$collection = new Collection([
['big' => 'bear'],
['bigger' => 'whale'],
['small' => 'chicken'],
['smaller' => 'tardigrade']
]);
// true
$collection->contains('big', 'bear');
// true
$collection->contains('smaller', 'tardigrade');
// false
$collection->contains('smaller', 'paramecium');
示例 3:用于对象集合,使用假设的用户 class:
use Illuminate\Support\Collection;
class User
{
/**
* A hypothetical user's first name.
*
* @var string
*/
public $firstName = '';
/**
* A hypothetical user's favorite number.
*
* @var integer
*/
public $favoriteNumber = 0;
public function __construct($firstName, $favoriteNumber)
{
$this->firstName = $firstName;
$this->favoriteNumber = $favoriteNumber;
}
}
// Create a new collection instance.
$collection = new Collection([
new User('Jane', 7),
new User('Sarah', 9),
new User('Ben', 2)
]);
// true
$collection->contains('firstName', 'Jane');
// false
$collection->contains('firstName', 'Josh');
// false
$collection->contains('lastName', 'Jane');
// true
$collection->contains('favoriteNumber', 2);
祝你好运;)
Laravel 文档:
has 方法确定给定键是否存在于集合中
https://laravel.com/docs/5.8/collections#method-has
contains 方法确定集合是否包含给定项目:
https://laravel.com/docs/5.8/collections#method-contains
因此 has 方法检查给定键是否在集合中,而 contains 方法检查给定值是否在集合中。
我无法理解 Laravel 收集方法 has()
和 contains()
之间的区别。
The contains() method takes a single value, a key-value pair of parameters or a callback function and returns a boolean value of the value is present in the collection or not.
所以基本上,它 returns 一个基于值存在的布尔值。
has() - returns a boolean value if a key value is present in a collection or not.
这也是returns一个基于值存在的布尔值?
不知何故我不明白它们的区别。
希望有人能给我解释一下或分享一些有用的链接,我将不胜感激。
has
用于键,contains
用于值。
$collection = collect(['name' => 'Desk', 'price' => 100]);
$collection->has('name'); // true
$collection->has('Desk'); // false
$collection->contains('name'); // false
$collection->contains('Desk'); // true
你好,我认为不同之处在于 has() 方法仅搜索键,例如:
$collection = collect(['account_id' => 1, 'product' => 'Desk', 'amount' => 5]);
$collection->has('product');
// true
$collection->has(['product', 'amount']);
// true
$collection->has(['amount', 'price']);
// false
and contains 方法() 用于确定集合中是否存在给定的$key。此外,可以指定一个可选的 $value,用于检查给定的 key/value 对是否存在于集合中。
示例 1:最基本的用法:
use Illuminate\Support\Collection;
// Create a new collection instance.
$collection = new Collection([
'bear',
'whale',
'chicken',
'tardigrade'
]);
// true
$collection->contains('bear');
// true
$collection->contains('tardigrade');
// false
$collection->contains('dog');
示例 2:使用 contains 方法检查给定的 key/value 对是否存在于包含数组作为项目的集合中:
use Illuminate\Support\Collection;
// Create a new collection instance.
$collection = new Collection([
['big' => 'bear'],
['bigger' => 'whale'],
['small' => 'chicken'],
['smaller' => 'tardigrade']
]);
// true
$collection->contains('big', 'bear');
// true
$collection->contains('smaller', 'tardigrade');
// false
$collection->contains('smaller', 'paramecium');
示例 3:用于对象集合,使用假设的用户 class:
use Illuminate\Support\Collection;
class User
{
/**
* A hypothetical user's first name.
*
* @var string
*/
public $firstName = '';
/**
* A hypothetical user's favorite number.
*
* @var integer
*/
public $favoriteNumber = 0;
public function __construct($firstName, $favoriteNumber)
{
$this->firstName = $firstName;
$this->favoriteNumber = $favoriteNumber;
}
}
// Create a new collection instance.
$collection = new Collection([
new User('Jane', 7),
new User('Sarah', 9),
new User('Ben', 2)
]);
// true
$collection->contains('firstName', 'Jane');
// false
$collection->contains('firstName', 'Josh');
// false
$collection->contains('lastName', 'Jane');
// true
$collection->contains('favoriteNumber', 2);
祝你好运;)
Laravel 文档:
has 方法确定给定键是否存在于集合中 https://laravel.com/docs/5.8/collections#method-has
contains 方法确定集合是否包含给定项目: https://laravel.com/docs/5.8/collections#method-contains
因此 has 方法检查给定键是否在集合中,而 contains 方法检查给定值是否在集合中。