gpt4 book ai didi

php - 在 Laravel 中搜索数组

转载 作者:行者123 更新时间:2023-12-05 09:17:20 24 4
gpt4 key购买 nike

我得到了两个数组并将它们合并,然后在 name 下寻找某个值,但似乎无法让它工作

$temp = [
['id' => 3, 'name' => 'Taylor'],
['id' => 3, 'name' => 'Abigail'],
];

$temp1 = [
['id' => 3, 'name' => 'Taylor'],
['id' => 3, 'name' => 'Taylor'],
];

$ggg = array_merge($temp,$temp1);

最佳答案

使用 contains()集合的方法。架构:

collect($array1)               // Create a collection from the first array
->merge($array2) // merge it with the second array
->contains($key, $value); // returns boolean

您的代码:

$temp = [
['id' => 3, 'name' => 'Taylor'],
['id' => 3, 'name' => 'Abigail'],
];

$temp1 = [
['id' => 3, 'name' => 'Taylor'],
['id' => 3, 'name' => 'Taylor'],
];

collect($temp)->merge($temp1)->contains('name', 'Taylor'); // true
collect($temp)->merge($temp1)->contains('name', 'Jane'); // false

如果要获取符合条件的项目,请使用 where() :

$result = collect($temp)->merge($temp1)->where('name', 'Taylor');

这将返回:

Collection {#465 ▼
#items: array:3 [▼
0 => array:2 [▼
"id" => 3
"name" => "Taylor"
]
2 => array:2 [▼
"id" => 3
"name" => "Taylor"
]
3 => array:2 [▼
"id" => 3
"name" => "Taylor"
]
]
}

关于php - 在 Laravel 中搜索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48432147/

24 4 0
文章推荐: 仅当定义了值时才更新 Python 字典
文章推荐: MPI:使用阻塞发送和接收有什么好处?
文章推荐: python-3.x - Python Selenium 错误 - 在 (207.1666717529297,25) 处不可点击,因为另一个元素
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com