kigogu Posted June 12, 2012 Share Posted June 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted June 12, 2012 Share Posted June 12, 2012 array[0][1] wouldn't work for obvious reasons. $str = array( 0 => array( 'something1' => array('stuff', 'stuff'), 'something2' => array('more stuff', 'more stuff') ) ); echo $str[0]['something2'][0]; // prints 'more stuff' Quote Link to comment Share on other sites More sharing options...
kigogu Posted June 12, 2012 Author Share Posted June 12, 2012 ...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? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted June 12, 2012 Share Posted June 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
kigogu Posted June 12, 2012 Author Share Posted June 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 foreach($arr AS $k=>$v){ echo $k; print_r($v); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.