aspirinemaga
aspirinemaga

Reputation: 3947

How to force array key's values to be integer instead of string when they are numeric?

How can I force all numeric values to be integer instead of string when some PHP function takes place for exemple with array_replace() ? here is an example:

My $item is an array of default values, which var_dump($item) produces this:

array (size=12)
  'id' => string '' (length=0)
  'cid' => int 2
  'pid' => string '' (length=0)
  'rid' => string '' (length=0)
  'section' => int 0
  'title' => string '' (length=0)
  'slug' => string '' (length=0)
  'image' => string '' (length=0)
  'description' => string '' (length=0)
  'ordering' => string '' (length=0)
  'created' => string '' (length=0)
  'modified' => string '' (length=0)

Then, i call a function to update $item array with new values which comes from db with a function array_replace($item, $item_db);, and when I var_dump($item) again, i get this:

array (size=12)
  'id' => string '12' (length=2)
  'cid' => string '1' (length=1)
  'pid' => string '0' (length=1)
  'rid' => string '37' (length=2)
  'section' => string '0' (length=1)
  'title' => string 'Article2' (length=8)
  'slug' => string 'articles123' (length=11)
  'image' => string 'e9213e52d235bd892b3337fce3172bed.jpg' (length=36)
  'description' => string '' (length=0)
  'ordering' => string '3' (length=1)
  'created' => string '2014-05-15 14:51:10' (length=19)
  'modified' => string '2014-05-15 23:29:40' (length=19)

I want to be all numeric values (id, cid, pid, rid, section, ordering) the integer, except created and modified keys.

How I suppose to do that without manually write each time something like:

$item['section'] = (int) $item['section'];

Is there any solution for this ?

Upvotes: 0

Views: 1424

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111869

You can use such simple foreach loop:

foreach ($array as $k => $v) {
   if ($k != 'created' && $k != 'modified') {
       $array[$k] = (int) $v;
   }
}

This is of course if you are sure that all values are numeric so they can be converted to int. Otherwise you have to use:

foreach ($item as $k => $v) {
    if (is_numeric($v)) {
        $item[$k] = (int) $v;
    }
}

Upvotes: 3

Amal Murali
Amal Murali

Reputation: 76636

Create an array containing the values which you'd like to not be renamed. Then loop through your array — on each iteration, check if the current key is in the $defaults array. If it is not, push it into a new array ($results) with the current numeric offset as the key. If not, push it into the new array with the current key:

Something along the lines of:

$defaults = ['created', 'modified']; // Keys to be left untouched
$result = [];                        // Results array
$i = 0;                              // Numeric offset

foreach ($array as $key => $value) {
    if (!in_array($key, $defaults)) {
        $result[++$i] = $value;
    } else {
        $result[$key] = $value;
    }
}

print_r($result);

Upvotes: 1

Related Questions