PHP renaming array and covert from string to integer

50 views Asked by At

I have var_dump() variable $fruit_array as below:

array (size=3)
  0 => 
    array (size=1)
      'fruit_id' => string '2' (length=1)
  1 => 
    array (size=1)
      'fruit_id' => string '1' (length=1)
  2 => 
    array (size=1)
      'fruit_id' => string '3' (length=1)

I need to rename fruit_id to id and convert array string value to integer, example result:

array (size=3)
  0 => 
    array (size=1)
      'id' => int 2
  1 => 
    array (size=1)
      'id' => int 1
  2 => 
    array (size=1)
      'id' => int 3

How can I do that ? thanks

1

There are 1 answers

1
Andizer On BEST ANSWER

You can do something like this:

<?php
foreach ( $array as $k=>$v )
{
  $array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
  unset($array[$k]['fruit_id']);
}