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)? Commented May 15, 2017 at 6:28
  • Am using laravel. It won't work Commented May 15, 2017 at 6:30
  • no. This is not working. Commented May 15, 2017 at 6:34
  • Possible duplicate of how to convert array values from string to int? Commented May 15, 2017 at 6:41
  • ya.. this answer ids given already. Thank You so much Commented May 15, 2017 at 6:44

4 Answers 4

23

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

<?php
    $explode_id = array_map('intval', explode(',', $request->data));
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much . You made my day special
I have one more problem too
@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
19

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 Comment

I think this solution better than the explode
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 Comments

I used it. But there is no change in the output
" is not a string part of output ["1","2","3","4","5","6"]
I have edited the answer. Did you try the second method?
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.