[eluser]TomTom[/eluser]
Hello everyone,
Apologies for a long post.
I have a multidimensional array which, in order to display its values in a view, should go through two foreach loops, like so:
Code:
<?php foreach ($content['id'] as $key1 => $taxon): ?>
<?php foreach ($taxon as $key2 => $instance): ?>
<pre><?php print_r($instance); ?></pre>
<?php $name = $instance['taxon_field']; ?>
<?php echo 'Value of the $name variable is: ' . $name; ?>
<?php endforeach; ?>
<?php endforeach; ?>
The above code outputs the following result:
Code:
Array
(
[unit_id] => 1
[unit_author] => 0
[unit_date] =>
[unit_parent_id] => 0
[unit_status] =>
[unit_type_id] => 1
[unit] => page_about
[content_id] => 1
[content_author] => 0
[content_date] =>
[content_parent_id] => 0
[content_status] =>
[content_taxon_id] => 1
[content_unit_id] => 1
[content] => Абаут
[taxon_id] => 1
[taxon_author] => 0
[taxon_date] =>
[taxon_field] => form_input
[taxon_is_multiple] => 0
[taxon_parent_id] => 0
[taxon_status] =>
[taxon_type_id] => 1
[taxon] => page_title
[type_id] => 1
[type_author] => 0
[type_date] =>
[type_parent_id] => 0
[type_status] =>
[type] => page
)
Value of the $name variable is: form_input
. Now, I actually need to convert that variable value into a function name. I know that should be done with
Code:
<?php $name(); ?>
(I also know there are other ways, but either one should work equally good and either one returns the same error atm). Now, if I try to do this:
Code:
<?php foreach ($content['id'] as $key1 => $taxon): ?>
<?php foreach ($taxon as $key2 => $instance): ?>
<pre><?php print_r($instance); ?></pre>
<?php $name = $instance['taxon_field']; ?>
<?php echo $name(); ?>
<?php endforeach; ?>
<?php endforeach; ?>
(note that the only difference are the parentheses in the variable name, which should turn it into a function), I get the following:
Code:
page
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'taxon_field'
Filename: views/edit.php
Line Number: 14
Fatal error: Call to undefined function p() in /views/edit.php on line 15
As it is obvious, when I added the () parentheses to the $name variable, for some reason the $instance array turned into a string with the value of the last array member $instance['type'] => page.
I tried to create another single-dimensioned array, with keys and values identical to those of $instance, and this time there were no errors. Can it be that the loops are creating some kind of conflict?
Any assistance is much appreciated.