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

5
  • Are you trying to do a IN (1,2,3,4,5)?
    – user1897253
    May 15, 2017 at 6:28
  • Am using laravel. It won't work
    – Kayal
    May 15, 2017 at 6:30
  • no. This is not working.
    – Kayal
    May 15, 2017 at 6:34
  • Possible duplicate of how to convert array values from string to int?
    – Qirel
    May 15, 2017 at 6:41
  • ya.. this answer ids given already. Thank You so much
    – Kayal
    May 15, 2017 at 6:44

4 Answers 4

22

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

<?php
    $explode_id = array_map('intval', explode(',', $request->data));
4
  • 2
    Thank you so much . You made my day special
    – Kayal
    May 15, 2017 at 6:35
  • I have one more problem too
    – Kayal
    May 15, 2017 at 6: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, 2017 at 7:16
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);
1
  • I think this solution better than the explode Mar 19, 2019 at 8:22
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

3
  • I used it. But there is no change in the output
    – Kayal
    May 15, 2017 at 6:27
  • " is not a string part of output ["1","2","3","4","5","6"]
    – lkdhruw
    May 15, 2017 at 6:29
  • I have edited the answer. Did you try the second method?
    – Shyamali
    May 15, 2017 at 6:29
0

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

 $a = "1,2,3,4,5";
 $b= preg_split("/[,]/",$a);
 print_r($b);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.