I used below code but that gives me an Error like "Notice : Undefined offset: 27" . Why these error generated or what these error means ? please any one help me.
//Code ... final_display2 is found some array.
for($p=0; $p<sizeof($final_display2); $p=$p+2){
$b = $p+1;
$len1 = strlen($final_display2[$p])+strlen($final_display2[$b]); //these line gives error 116
if($len1 < 3200){
array_push($final_display,$final_display2[$p].$final_display2[$b]); //and these one also 118 }else{
array_push($final_display,$final_display2[$p]);
array_push($final_display,$final_display2[$b]);
}
}
ERROR!!
Your error occurs when $p==sizeof($final_display2)-1, so $b==sizeof($final_display2), which will result in a Undefined Offset.
ie. if sizeof($final_display2)==27, when $p==26, then $b==27 and $final_display2[$b]/$final_display2[27] does not exist.
Try using if(isset($final_display2[$b])) ->
for($p=0; $p<sizeof($final_display2); $p=$p+2){
$b = $p+1;
if(isset($final_display2[$b])){ //only run if $final_display2[$b] exists
$len1 = strlen($final_display2[$p])+strlen($final_display2[$b]); //these line gives error 116
if($len1 < 3200){
array_push($final_display,$final_display2[$p].$final_display2[$b]); //and these one also 118 }else{
array_push($final_display,$final_display2[$p]);
array_push($final_display,$final_display2[$b]);
}
}
}
Just use $p instead of $b since your looping trough all items in $final_display2, but your adding 1 to $b it's an offset! (Also change in your for loop this $p=$p+2 to $p++)
Little example:
You have 5 items in $final_display2 so the loop is like this:
for($p=0; $p<5; $p++)
//So if your at the last element ($p = 4) but your adding 1 to $b and use it you have a offset
Edit:
I think your looking for something like this:
foreach($final_display2 as $k => $v) {
if( isset($final_display2[$k+1]) ) {
if($k == 0 || $k % 2 == 0) {
$len1 = strlen($final_display2[$k])+strlen($final_display2[$k+1]);
if($len1 < 3200){
array_push($final_display,$final_display2[$k].$final_display2[$k+1]);
array_push($final_display,$final_display2[$k]);
array_push($final_display,$final_display2[$k+1]);
}
}
}
Related
I get this Error for every row in the data. So around 500 times, but every time with an other Undefined offset.
Heres my Code:
$fl_array = preg_grep('/^\d+\s('. $SelectedTime .':)\d+/', explode("\n", $finalLog));
$count = count($fl_array);
for ($x = 0; $x < $count; $x++)
{
echo "$fl_array[$x] \n";
}
as written here - http://php.net/manual/en/function.preg-grep.php: Returns an array indexed using the keys from the input array. so maybe smth wrong with your keys:
if (isset($fl_array[$x])) {
echo "$fl_array[$x] \n";
}
I'm having this error when I run my program:
Notice: Uninitialized string offset: 7 in C:\xampp\htdocs\demo\str_rev.php on line 21
What causes that?
<?php
//strrev($arg);
/*$str = "ademola";
echo strrev("$str");
*/
function reverse_String($str){
$i = 0;
while(!empty($str[$i])){
echo $str[$i];
$i++;
}
for($r = $i; $r > -1; $r--){
$reverse = $str[$r];
echo $reverse;
}
}
reverse_String("Ademola");
?>
Output:
Ademola
Notice: Uninitialized string offset: 7 in C:\xampp\htdocs\demo\str_rev.php on line 21
alomedA
The $i++; in your first while loop increments $i to 7 in its last iteration. The condition !empty($str[$i]) is no longer satisfied, so the loop does not execute again, but $i is still 7 when the next loop starts, which is an index beyond the end of the string.
There are various ways to fix this, a simple way is to subtract 1 from the counter when you define your second loop to set $r to the index of the last character in the string.
for($r = $i - 1; $r > -1; $r--){ ...
As mentioned by Don't Panic there are many ways to fix this,
you can use isset as:-
for($r = $i; $r > -1; $r--){
if(isset($str[$r])) {
$reverse = $str[$r];
echo $reverse;
}
}
Or to reverse the string you can simply use the php's built in function (strrev)
echo strrev('Ademola')
I'm getting an "Undefined offset" PHP error, and don't get what causes it. I'm just a beginner.
Notice: Undefined offset: 3 on line 58
Here's code I'm using:
$file = fopen("portfolio.file", "r") or die("Unable to open a portfolio file.");
$portfolioFull = fread($file,filesize("portfolio.file"));
fclose($file);
$portfolioItems = explode(";", $portfolioFull);
$i = count($portfolioItems);
echo $i;
while ($i >= 0){
$portfolio[$i] = explode("||", $portfolioItems[$i]);
$i = $i - 1;
}
echo $portfolio[1][0];
echo $portfolio[1][1];
echo $portfolio[1][2];
echo $portfolio[2][0];
echo $portfolio[2][1];
echo $portfolio[2][2];
Here's what portfolio file contains:
Item 1 Title
||
Item 1 Description
||
DOWNLOAD PENDING
;
Item 2 Title
||
Item 2 Description
||
DOWNLOAD UNAVAILABLE
;
Test Item
||
Test Description
||
DOWNLOAD AVAILABLE
And here's what debug echo says: https://gyazo.com/2e1a6e90f1a33578b40e5f330e19dd78
Any clues how to fix that?
$i = count($portfolioItems);
echo $i;
$i-- ; //reduce by one should fix the problem
while ($i >= 0){
$portfolio[$i] = explode("||", $portfolioItems[$i]);
$i = $i - 1;
}
Since the index of an array start from zero, the value of the last element will be count($array) -1.
In this case you are using the count value , which will be 1 greater than the last index. Ex: if array has 3 elements , the indexes will be 0 , 1, 2 . Trying to use 3 as an index will give you this error.
I am trying to split the Content that is fetching from DB a few words next to image and a remaining content in the below paragraph
I am getting undefined offset error : 41
the below is the code... it executes correctly but with notice error as above... Can you please help me to fix it.
<div class="contentfont" style="padding-left:20px; text-align:left;">
<?php
$words = explode(" ",$dcontent);
$cntWords = count($words);
$splitWords = round($cntWords/2);
for ($i=0;$i<=$splitWords;$i++)
{
print $words[$i].' ';
$halfPoint = $i;
}
?>
<p class="contentfont" style="padding-top:10px;">
<?php
for ($i=$halfPoint;$i<=$cntWords;$i++)
{
print $words[$i].' ';// This print statement is causing me this error Notice: Undefined offset: 41 in D:\xampp\htdocs\training\admin-panel\php\sai_devotees_detail.php on line 106
}
?>
</p>
Undefined offset in this case means you are reading past the length of the $words array. Try $cntWords - 1 for ceiling of the loop like this:
for ($i = $halfway; $i <= $cntWords -1; $i++) {
$halfPoint = $i; is also outside the loop. Try this instead:
for ($i = 0; $i <= $splitWords; $i++) {
print $words[$i] . ' ';
$halfPoint = $i;
}
When you make a loop in PHP without brackets, only the first statement after it will be executed. So in this case, $i doesn't exist for you to assign it to $halfPoint. Don't think it's clever to compact everything into one line. It will only cause you problems.
i wrote this code and i'm getting this error, i tried every this but doesn't seem to work btw i've made a comment for line 55 where i'm gettin error
function calculate_result()
{
$option_number = array('option_a'=>'1','option_b'=>'2','option_c'=>'3','option_d'=>'4');
$answers = array();
$total_questions = $this->quiz_model->return_number_of_questions($this->input->post('quiz_number'));
if($total_questions > 0)
{
for($i=1; $i <= $total_questions; $i++)
{
//line 55 $answers[$i] = $option_number[$this->input->post('question_'.$i)];
}
print_r($answers);
}
else
{
show_404();
}
//print_r($answers);
}
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: controllers/quiz.php
Line Number: 55
Change this...
for($i=1; $i <= $total_questions; $i++)
To this...
for($i=0; $i <= $total_questions; $i++)
On that line there are two indexes, on the left side, there is $i and it is non-zero.
So, problems has to be with right side expression, with $this->input->post('question_'.$i) which probably returns 0.