Jump to content

array suntax...


jimfog

Recommended Posts

here is a piece of code-related to arrays- which i am having difficulty understanding it and i would appreciate some help

for($=0;$i<$lists;$i++){ echo ....$list[i][0] }

There is also some other code but the above is enough for what i want to ask. Why $list[0] has both [0] in front of it? The 0 is to access the first element of the array, what is the role of the loopcounter i there? I think it is there to access all the elements of the array one by one but how exactly does it achieve that by having the counterleft from the array index--[0]?

Link to comment
Share on other sites

It should just be $list[$i]. The "i" needs the $ symbol next to it. As for the [0]. It would access the first element of the array contained within $link[$i]

Link to comment
Share on other sites

well, it's actually the code that you are leaving out that is important. We can infer the structure of $lists because of the loop, which appears to be multidemensional. A simple way to confirm for yourself is just to output $list. this is a simple appx. of what $list might look like that would work for that loop.

$list = array(  0 => array(0 => 'somevalue'),  1 => array(0 => 'someothervalue'),  2 => array(0 => 'yetsomeothervalue'));

edit: and yes, the loop counter would need to be $i

Edited by thescientist
Link to comment
Share on other sites

  • 2 weeks later...
well, it's actually the code that you are leaving out that is important. We can infer the structure of $lists because of the loop, which appears to be multidemensional. A simple way to confirm for yourself is just to output $list. this is a simple appx. of what $list might look like that would work for that loop.
$list = array(  0 => array(0 => 'somevalue'),  1 => array(0 => 'someothervalue'),  2 => array(0 => 'yetsomeothervalue'));

edit: and yes, the loop counter would need to be $i

Yes it must be a multidimensional array.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...