Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to array conversion in php

Tags:

php

laravel

I got the string from the ajax and passed the string in url as 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"]

But I want the data as: [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:

  $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

$("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

like image 699
Kayal Avatar asked Nov 28 '22 05:11

Kayal


2 Answers

@Kayal try it with array_map() with inval like below:

<?php
    $explode_id = array_map('intval', explode(',', $request->data));
like image 56
Vishal Solanki Avatar answered Dec 05 '22 23:12

Vishal Solanki


You can just json_decode it.

$explode_id = json_decode($request->data, true);

Also, if you're trying to do a IN() condition, you can use:

->whereIn('id', $explode_id);
like image 39
Khoon Avatar answered Dec 05 '22 23:12

Khoon