0

I have array named data in that i have sub array as place_id,when I print place_id from data array as

dd($data['place_id'])

It gives me result as

 array:1 [
   0 => "7"
   1 => "3"
 ]

I want values to be of type int as follows:

array:1 [
       0 => 7
       1 => 3
     ]

How will I get this result ?

2 Answers 2

1

You can use intval() with array_map():

$data['place_id'] = array_map('intval', $data['place_id']);
Sign up to request clarification or add additional context in comments.

1 Comment

you should quote the intval function
1

There is plenty of options:

1.Using foreach:

    foreach ($data as $k => $v) {
        $data[$k] = intval($v);
    }

    dd($data);

Result:

array:2 [
  0 => 7
  1 => 3
]

2.Using array_map() which is also for loop at the end:

 $data['place_id'] = array_map('intval', $data['place_id']);
 dd($data);

Result:

array:2 [
  0 => 7
  1 => 3
]

Comments

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.