0

Suppose after applying implode to an array , I got a String for example :-

$var = 1631075,1631076;

On applying var_dump to $var , I received the output as string(15) "1631075,1631076"

how will I convert entire $var into Integer variable . Such that the var_dump display int(15)1631075,1631076

8
  • 1
    That doesn't look like a valid integer. Do you want to get the integer 16310751631076? Commented Jun 8, 2018 at 17:17
  • I dont understand your question, do you want to create an array of integer? Commented Jun 8, 2018 at 17:18
  • No actually I want to get an integer as :- 1631075,1631076 Commented Jun 8, 2018 at 17:21
  • 1
    A valid integer will not include and commas only numbers. You format the integer->string to show commas for easy readability later. Strip out any non-numbers and do $var = intval($string_with_only_numbers); Commented Jun 8, 2018 at 17:21
  • Plus, that comma is not in the correct place as a thousands separator. Commented Jun 8, 2018 at 17:24

1 Answer 1

1
$var = '1631075,1631076';
foreach(explode(',',$var) as $val){
 echo intval($val);
}

or something like

$var = '1631075,1631076';
$integers = explode(',',$var);
$converted = array_map('intval', $integers);
Sign up to request clarification or add additional context in comments.

4 Comments

But Sir ,it is giving the output as 16310751631076; how can I get 1631075,1631076 as integer
"1631075,1631076" is not an integer and can't be. That can only be a string if you want that inside one variable.
Sir actually I want the final output as [1631075,1631076] (integer format) the required array is Array ( [0] => 1631075 [1] => 1631076 )
Use "intval()" on the array values before handling mathematical functions. Also look at stackoverflow.com/questions/8963910/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.