7

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

Kayal
  • 137
  • 3
  • 3
  • 11

4 Answers4

22

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

<?php
    $explode_id = array_map('intval', explode(',', $request->data));
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
  • 2
    Thank you so much . You made my day special – Kayal May 15 '17 at 06:35
  • I have one more problem too – Kayal May 15 '17 at 06:46
  • @Kayal you are turning question to wrong way, previous and current cases are different, i really suggest you to ask a new question for that with your full ajax code with more explanation because your question stuff is not sufficient to answer your question – lazyCoder May 15 '17 at 07:16
  • http://stackoverflow.com/questions/43974228/ajax-data-update-for-each-ajax-function – Kayal May 15 '17 at 08:07
18

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);
0

You can use str_replace('"','',$string[$i]); inside a for loop for each array element or else you can do it as

$intVal = (int)$string[$i];

in side a for loop

Shyamali
  • 329
  • 7
  • 22
0

I'd do it this way, try this...

 $a = "1,2,3,4,5";
 $b= preg_split("/[,]/",$a);
 print_r($b);
Red Bottle
  • 2,839
  • 4
  • 22
  • 59