PHP array object cast integer with array_map or array_walk

one text

Solution:

Put the values of each sub array through intval:

<?php
$data =
[
    [
        'status' => '1',
        'total'  => '2',
    ],
    [
        'status' => '3',
        'total'  => '4'
    ],
];

$result = array_map(fn($v)=>array_map('intval', $v), $data);

var_dump($result);

Output:

array(2) {
  [0]=>
  array(2) {
    ["status"]=>
    int(1)
    ["total"]=>
    int(2)
  }
  [1]=>
  array(2) {
    ["status"]=>
    int(3)
    ["total"]=>
    int(4)
  }
}

Or just transform the leaves in-place:

array_walk_recursive($data, function(&$v) { $v = (int) $v; });

And a simple foreach with type juggling:

foreach($data as &$v) {
    $v['status']+=0;
    $v['total'] +=0;
}
unset($v);

Source