1

I've been looking into this problem for a couple of days now and I just can't seem to figure it out.

I'm trying to do something simple, I thought, just looping through an array. This is a screenshot of the array: http://cl.ly/image/3j2J3x1C3B0j I'm trying to loop through all the 'Skills' array, there the "Skill' array and inside that grabbing the "Icon". For this I made 2 loops:

    foreach ($hero_data['skills'] as $skills)
            {
                foreach ($skills as $skill)
            {
                //print_r($skill['skill']);
            }
    }

Unfortunaly this doesn't work, in laravel. I'm getting the "Undefined index: skill" error. It does work when I tried it outside , as a standalone script.

Out side both of the loops I can select the icon with:

    print_r($hero_data['skills']['active'][0]['skill']['icon']);

I'm sure I'm overlooking something stupid...

Thanks a lot for the help,

1
  • Can you post your full $hero_data array? You are probably looping over elements that don't have the required keys but they aren't visible in your screenshot. Commented Oct 15, 2012 at 17:19

4 Answers 4

2

Looking at what you've said from the other solutions posted here, it's clear that you are looping through the sub arrays and not all of those sub arrays contain the keys that your further loops are looking for.

Try this:

foreach ($hero_data['skills']['active'] as $skills) {
    if (isset($skills['skill']['icon'])) {
        print_r($skills['skill']['icon']);
    }
}

Because, for example, if $hero_data['skills']['active'][8] doesn't actually have a skill array or a ['skill']['icon'] array further down, then the loop will throw the errors you have been reporting.

The nested array keys you are looking for must be found in every iteration of the loop without fail, or you have to insert a clause to skip those array elements if they aren't found. And it seems like your $hero_data array has parts where there is no ['skill'] or ['icon'], so therefore try inserting one or more isset() checks in the loops. Otherwise, you need to find a way of guaranteeing the integrity of your $hero_data array.

Your game looks interesting by the way!

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

2 Comments

This fixed it. Thank you for the excellent explanation, I understand now and hope to never mike this mistake again. :) Thanks again!
Oh yes, this is data retrieved from the Diablo 3 API. :)
0

Inside skills you have an 'active' attribute and it contains the array you need, so you need to change your code to this:

 foreach ($hero_data['skills'] as $skills)
            {
                foreach ($skills['active'] as $skill)
            {
                //print_r($skill['skill']);
            }
    }

1 Comment

I'm getting "Undefined index: active" when doing that. :(
0

Try:

foreach ($hero_data['skills'] as $skills)
{
  foreach ($skills as $skillState)
  {
    foreach ($skillState as $skill)
    {
      print_r($skill['skill']);
    }
  }
}

1 Comment

Same error using this code. It's so weird this doesn't work :/
0

You simply need to iterate the active index of the array. this should work :

foreach ($hero_data['skills']['active'] as $skills) {
    print_r($skills['skill']['icon']);
}

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.