【发布时间】:2021-03-30 23:26:18
【问题描述】:
我有这样的数据数组:
[
{
fruit: "Apple",
price: "10",
},
{
fruit: "Orange",
price: "12",
},
{
fruit: "Manggo",
price: "14",
},
{
fruit: "Grape",
price: "16",
},
{
fruit: "Apple",
price: "13",
},
]
如何从我的数据中使用搜索方法来查找具有输入类型的数据?在此之前,我想展示我所做的,这是我的代码:
$search = "Apple"; // let's just say the value of this variable comes from the input type
$myData = [
[fruit: "Apple", price: "10"],
[fruit: "Orange", price: "12"],
[fruit: "Manggo", price: "14"],
[fruit: "Grape", price: "16"],
[fruit: "Apple", price: "13"],
];
$getSearch = collect($myData)->where('fruit', $search)->all();
var_dump($getSearch);
对于输出,我没有从我的结果中得到任何东西,就像这样:
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
为了期待,我的结果变成了这样:
[
{
fruit: "Apple",
price: "10",
},
{
fruit: "Apple",
price: "13",
},
]
【问题讨论】:
标签: arrays laravel search collections