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?

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.