Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6331627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:06:22+00:00 2026-05-24T18:06:22+00:00

Getting an Undefined Offset error here — apparently from the $newval array. Note that

  • 0

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;

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:06:23+00:00Added an answer on May 24, 2026 at 6:06 pm

    Before you can concatenate to a variable, that variable must exist (to avoid a Notice). Simply declare $newval[$key] as an empty string before the for loop:

    foreach ($bags as $key => $value) {
        $newval[$key] = '';
        for ($i = 0, $s = strlen($value); $i < $s; $i++) {
            if ( is_numeric($value[$i]) ) {
                $newval[$key] .= $value[$i];
            }
        }
    }
    

    By the way, there’s nothing wrong with your starting value of $i. It is correct to have it at 0 and not 1 as 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 for loop and simply:

    foreach ($bags as $key => $value) {
        $digits = preg_replace('/[^0-9]/', '', $value);
        if (strlen($digits)) {
            $newval[$key] = $digits;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you easily avoid getting this error/notice: Notice: Undefined offset: 1 in /var/www/page.php
I'm not sure why I am getting an Undefined Offset Notice on this: <?php
I am getting JQUERY undefined error. Do you have any idea (code below). <%@
I'm getting an Undefined Instruction error while running an embedded system, no coprocessor, no
Why am I getting an undefined label error in following code? (I am leaving
I have a problem where a method is getting an undefined variable error, even
I keep getting an Notice: Undefined index: did error with this query, and I'm
Trying to compile a iPhone/iPad application with SDK3.2 and am getting this error: Undefined
Getting Undefined index: filename error in the below image upload php code. Is there
I am getting undefined method [] for true:TrueClass error while running the rake task

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.