4

As stated in the title, I encounter a strange problem for the following code.

 for($i=0; $i < count($maindata) ; $i++){
     $currentId = $maindata[$i]['SubmissionId'];                            
     $output = array_filter($subdata, function ($value) use ($currentId) 
            { return $subdata['ParentId'] == $currentId; });

     echo 'total sub data '.count($output); //output 86

     for($j=0; $j < count($output) ; $j++){
         echo $output[$j]['SubmissionId'];  //Undefined offset Error                       
     }
}

As you can see, I loop through an array. Inside the loop I filter out another array to get the associate data.

And then I echo the count of the filter array and echo the data with another loop. The code was fine for first item and it show data but starting from the second item, it show

Notice: Undefined offset: 0 in myfile on line 79

inside the second loop but the count still showing correct answer also. line 79 is echo $output[$j]['SubmissionId']; //Undefined offset Error .

Please help me to figure out what is the problem. Thanks in advance.

3
  • 2
    to remove your error you need to do if(isset($output[$j]['SubmissionId'])){echo $output[$j]['SubmissionId'];}. More you have to print $output before second iteration and show us the data. May be some correction required Commented Jul 21, 2016 at 8:04
  • please put the full error message. Add a sample of the $output array Commented Jul 21, 2016 at 8:04
  • That is hardly what one would call a full error trace. Usually they are accomopanied by a line nunber too. For all we know the error might not even be on the line that you think it is Commented Jul 21, 2016 at 8:05

3 Answers 3

2

Solution:-

Inside second iteration use this:-

if(isset($output[$j]['SubmissionId'])){echo $output[$j]['SubmissionId'];}

Explanation:-

$output is a filtered sub-array and due to filtration it's quite possible that some indexes are missing

when you did for($j=0; $j < count($output) ; $j++){ and if count($output) is 86 means loop will go for 0-85 (in sequential order)

Now if $output has missed index 3 (for example) then the code $output[$j]['SubmissionId'] will give error

So check that using above code and problem is solved.

2nd opinion:-

you can do like below:-

$output = array_values($output);
echo 'total sub data '.count($output); //output 86

for($j=0; $j < count($output) ; $j++){
 echo $output[$j]['SubmissionId'];
}

Note:- this approach will re-index your array and if some-where you are going to use original indexes then this code will lead you to problems.

3rd option(better than 2nd one)

Since foreach() take care of indexes so instead of for() loop use foreach.

Thanks

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

Comments

2

In first place , i think that "foreach" loop instead of "for" will be more clear and a better practice for what you need.

http://php.net/manual/fr/control-structures.foreach.php

In a second time, do you try to make a var_dump instead of echo to see what structure you have into your array?

for($i=0; $i < count($maindata) ; $i++){
 $currentId = $maindata[$i]['SubmissionId'];                            
 $output = array_filter($subdata, function ($value) use ($currentId) 
        { return $subdata['ParentId'] == $currentId; });

 var_dump($output);exit();

 .....

Maybe you don't have SubmissionId sub-index inside.

1 Comment

Yes. foreach was my problem.
1

I found the problem by printing out the array as @Anant mention. At first I thought $output arrary's index will be starting from zero as normal array. But, since I got this from array_filter, the index starting from the end of last filtered array.

So I change the second loop to foreach and problem solved now.

Thank you all!!!

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.