Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to use regular expressions. </p> <p>use <code>str_replace()</code> as you need to replace the <code>','</code> for a <code>'.'</code>, and then use <code>intval()</code> or <code>floatval()</code> functions to get the numeric value. You can also use <code>strstr()</code> to look for the <code>'.'</code> and decide if using <code>intval()</code> or <code>floatval()</code></p> <p>Example:</p> <pre><code>$row = array('Unspecified risk', 'Yes', '8', '3', '2', '13', 'none', '-1,49', '-2,51', '-1,46', '-1,54'); function toNumber($target){ $switched = str_replace(',', '.', $target); if(is_numeric($target)){ return intval($target); }elseif(is_numeric($switched)){ return floatval($switched); } else { return $target; } } $row = array_map('toNumber', $row); var_dump($row); </code></pre> <p>We use str_replace() to replace the dot for the comma, this way it's a international notation float, even if it's on string, this way later on we can check if it's numeric with <code>is_numeric()</code> &lt;-- this function is awesome as it detects from a string if it's a number or not, no matter integer or float etc.</p> <p>We use the is_numeric to check if the value is integer float or text and return the corresponding value using <code>intval()</code> or <code>floatval()</code> (the value without the replace applied will not return as a valid numeric, only after switching the , and . it will return true as numeric).</p> <p>We use <code>$row = array_map('toNumber', $row);</code> to apply the changes to the array.</p> <p>Profit xD</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload