Jump to content

Array Navigation


kigogu

Recommended Posts

For some reason this is stumping me =__= but I am having troubles navigating through an array.

My array is set up like:

array( 
      0 => array(
                "something1" => array("stuff" , "stuff")
                "something2" => array( "more stuff", "more stuff"))

i've tried doing a foreach on the array but it only pulls "something1" and not "something2" for some reason, and when i try array[0][1] to try and get "something2" it just says i'm out of the index.

Link to comment
Share on other sites

something1 and something2 are keys of the array at the [0] key of your first array. 
There is no [0][1]. It'd be [0]["something1"]. 
The foreach would have to be two levels deep, a foreach within a foreach, unless you do the foreach ON array[0], instead of array.

Link to comment
Share on other sites

...ok i'm retarded xD bleh, thanks you two~ X_x but then why wouldn't a foreach work for it?

How would i be able to get that key though? without also knowing the value associated with the key?

 

for example, I only know '0' but not 'something1' or 'something2' nor the values that point to those keys?

Link to comment
Share on other sites

There should never really be a point in which you are populating an array with keys/values that are completely unknown.  Know what I mean?

 

Do you have an application that you are currently working on?  Post some code and the intentions behind the code.

 

You don't need to know the value.  You need to know the key.  If you are, for whatever reason, unsure what the key's might be, stick with an indexed array instead of an associative array.  But I still fail to think of an application where one would not know the structure of their array.

Link to comment
Share on other sites

i figured it out, mainly just changed my array around so it wasn't a big deal. the reason i wouldn't know the key or the values is due to that the array could change to anything that a user might input in to it. the array was just a way to store the data temporarily and made it easier to just loop through the array to find the values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.