Jump to content

How to call elements within a variable array


ryandward

Recommended Posts

<?	

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";	
}
?>

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.