0

I have a variable $data in my controller. In which I am getting the following result

Array( [0] => 99 [1] => 100 [2] => 101 [3] => 102 [4] => 103)

Now I want to get the single entry one by one by using for loop. The main purpose of this loop is that I want to insert these values in my DB table.

 if(Yii::$app->request->isAjax && Yii::$app->request->post())
    {
        $data = explode(',',$_POST['data']);

       for($i=0; $i<=count($data); $i++)
        {
            print_r($data[$i]);
        }
        //print_r($data);
    }
    else{
        echo 'no data';
    }
    exit();

When i Run this one, I am getting the error

500 Internal Server Error PHP Notice 'yii\base\ErrorException' with message 'Undefined offset: 5'

Stack Trace

Stack trace:
#0 E:\xampp\htdocs\inventory-
web\backend\controllers\OgpheaderController.php(170): yii\base\ErrorHandler-
&gt;handleError(8, &#039;Undefined offse...&#039;, 
&#039;E:\\xampp\\htdocs...&#039;, 170, Array)
#1 [internal function]: backend\controllers\OgpheaderController-
&gt;actionViewsetpdf(&#039;55&#039;)
#2 E:\xampp\htdocs\inventory-
web\vendor\yiisoft\yii2\base\InlineAction.php(57): 
call_user_func_array(Array, Array)
#3 E:\xampp\htdocs\inventory-
web\vendor\yiisoft\yii2\base\Controller.php(156): yii\base\InlineAction-
&gt;runWithParams(Array)
#4 E:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\Module.php(523): 
yii\base\Controller-&gt;runAction(&#039;viewsetpdf&#039;, Array)
#5 E:\xampp\htdocs\inventory-
web\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module-
&gt;runAction(&#039;ogpheader/views...&#039;, Array)
#6 E:\xampp\htdocs\inventory-
web\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application-
&gt;handleRequest(Object(yii\web\Request))
#7 E:\xampp\htdocs\inventory-web\backend\web\index.php(17): 
yii\base\Application-&gt;run()
#8 {main}</pre>

I have searched for this error solution but couldn't find the correct answer(s).

Any help would be highly appreciated.

2
  • 1
    change to $i<count($data) Commented Nov 14, 2017 at 7:42
  • the count of data is 5 but the array start for 0 to 4 so ue < count($data) ..not <= Commented Nov 14, 2017 at 7:44

3 Answers 3

2

instead of the for loop, use foreach:

foreach($data as $d)
{
    print_r($d);
}

makes sure that you always stay within bounds of the array

Sign up to request clarification or add additional context in comments.

Comments

2
 for($i=0; $i<count($data); $i++)
    {
        print_r($data[$i]);
    }

or

 for($i=0; $i<=count($data)-1; $i++)
    {
        print_r($data[$i]);
    }

or

foreach($data as $value)
{
print_r($value);
}

any of these will work

Comments

1

change your code to

for($i=0; $i<count($data); $i++)
{
  print_r($data[$i]);
}

Array has total 5 elements which start with index 0 and end with 4 and when you compare count with <= it go to the index 5.

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.