Getting an Undefined Offset error here — apparently from the $newval array.
Note that the {exp} tag is not PHP, and is simply a sql query by my CMS system which creates the $bags array for me.
<?php
$bags = array();
$newval = array();
$pattern = "[^0-9]";
{exp:query sql="SELECT m_field_id_1 as bags FROM exp_member_data WHERE m_field_id_1 IS NOT NULL"}
$bags[] = "{bags}";
{/exp:query}
foreach ($bags as $key => $value) {
for ( $i = 0, $s = strlen($value); $i < $s; $i++) {
if ( is_numeric($value[$i]) ) {
$newval[$key] .= $value[$i];
}
}
}
$sum = array_sum($newval);
$format = number_format($sum);
echo $format;
?>
Before you can concatenate to a variable, that variable must exist (to avoid a
Notice). Simply declare$newval[$key]as an empty string before theforloop:By the way, there’s nothing wrong with your starting value of
$i. It is correct to have it at0and not1as others are suggesting.However, if you’re trying to remove non-number characters from a string and avoid empty array elements (as your original code does), you can remove the inner
forloop and simply: