0

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 ?

2 Answers 2

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

"I want to be all numeric values (id, cid, pid, rid, section, ordering) the integer, except created and modified keys."
I made a small change to your answer which actually works as expected. Pls consider to accept modification. Thank you for idea
Done, using is_numeric in this case will be better indeed.
1

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);

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.