ryandward Posted October 25, 2011 Share Posted October 25, 2011 <? $i=1; $analysts[$i] = array(0=>'alpha'); $regions[$i] = array(0=>'beta'); $countries[$i] = array(0=>'gamma'); $provinces[$i] = array(0=>'delta'); $events[$i] = array(0=>'epsilon'); $selectable_document_elements = array ( 0=> analysts, 1=> regions, 2=> countries, 3=> provinces, 4=> events ); foreach($selectable_document_elements as $value) { echo $value; print_r($$value[$i]); echo "</br>"; } ?> I can't seem to call $$value[$i], however when I leave off the [$i] part print_r($$value); I get the individual elements... analystsArray ( [1] => Array ( [0] => alpha ) ) regionsArray ( [1] => Array ( [0] => beta ) ) countriesArray ( [1] => Array ( [0] => gamma ) ) provincesArray ( [1] => Array ( [0] => delta ) ) eventsArray ( [1] => Array ( [0] => epsilon) ) However, I would like to get the 1 part of the array, in this example. Is there a magic trick to calling variable arrays' individual elements? Quote Link to comment https://forums.phpfreaks.com/topic/249756-how-to-call-elements-within-a-variable-array/ Share on other sites More sharing options...
watsmyname Posted October 25, 2011 Share Posted October 25, 2011 why are you assigning array to an array? you could simply do $analysts = array(0=>'alpha'); what is your desired result i didnt understand Quote Link to comment https://forums.phpfreaks.com/topic/249756-how-to-call-elements-within-a-variable-array/#findComment-1281985 Share on other sites More sharing options...
ryandward Posted October 25, 2011 Author Share Posted October 25, 2011 I am just assigning an array to an array, because I am testing whether or not I can build a more generic multidimensional array foreach loop, as opposed to writing it for each selectable_document_element, in my case I have around 25. Obviously in the code, where I plan to place it, the multidimensional arrays will be pulled as variables from the database. Quote Link to comment https://forums.phpfreaks.com/topic/249756-how-to-call-elements-within-a-variable-array/#findComment-1281987 Share on other sites More sharing options...
watsmyname Posted October 25, 2011 Share Posted October 25, 2011 I think this is what you want, please run it to confirm. <? $i=1; $analysts[$i] = array(0=>'alpha'); $regions[$i] = array(0=>'beta'); $countries[$i] = array(0=>'gamma'); $provinces[$i] = array(0=>'delta'); $events[$i] = array(0=>'iota'); $selectable_document_elements = array ( 0=> analysts, 1=> regions, 2=> countries, 3=> provinces, 4=> events ); foreach($selectable_document_elements as $value) { $val=$$value; echo "<pre>"; print_r($val[$i]); echo "</pre>"; echo "</br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249756-how-to-call-elements-within-a-variable-array/#findComment-1281990 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.