laravel php中字符串到数组的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43972688/
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
String to array conversion in php
提问by Kayal
I got the string from the ajax and passed the string in url as 1,2,3,4,5,6
我从 ajax 中获取字符串,并将 url 中的字符串作为 1,2,3,4,5,6 传递
After using explode function,
i.e. $explode_id = explode(',', $request->data);
I got the output as
["1","2","3","4","5","6"]
使用爆炸功能后,即$explode_id = explode(',', $request->data);
我得到的输出为 ["1","2","3","4","5","6"]
But I want the data as: [1,2,3,4,5,6]
但我希望数据为:[1,2,3,4,5,6]
How do I get it? And it should not be like ["1,2,3,4,5,6"] Because I want to compare the data in:
我如何得到它?它不应该像 ["1,2,3,4,5,6"] 因为我想比较以下数据:
$product_id = Product::where('name', 'like','%'.$main.'%')->where('id', $explode_id)->pluck('id');
I got the answer here. Am passing the id that i want to match in URL like
我在这里得到了答案。正在传递我想在 URL 中匹配的 ID,例如
$("input[type='checkbox']").click(function() {
if ($(this).is(':checked')) {
var days = $(this).val();
$.ajax({
type: "get",
url: '/getcreatedat',
data: { 'days': days },
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
var pageURL = $(location).attr("href");
window.location.href = pageURL+ '/' +data;
}
function errorFunc(xhr, error) {
console.log(error);
}
}
});
The next time I click the function: url passing the id's double the time
下次我点击函数时:url 传递 id 的时间是两倍
回答by Vishal Solanki
@Kayal try it with array_map() with inval like below:
@Kayal 尝试使用带有 inval 的 array_map(),如下所示:
<?php
$explode_id = array_map('intval', explode(',', $request->data));
回答by Neat
You can just json_decode
it.
你就可以json_decode
了。
$explode_id = json_decode($request->data, true);
Also, if you're trying to do a IN()
condition, you can use:
此外,如果您尝试执行IN()
条件,则可以使用:
->whereIn('id', $explode_id);
回答by Red Bottle
I'd do it this way, try this...
我会这样做,试试这个...
$a = "1,2,3,4,5";
$b= preg_split("/[,]/",$a);
print_r($b);
回答by Shyamali
You can use str_replace('"','',$string[$i]);
inside a for loop for each array element or else you can do it as
您可以str_replace('"','',$string[$i]);
在 for 循环内为每个数组元素使用,否则您可以这样做
$intVal = (int)$string[$i];
in side a for loop
在 for 循环侧