CodeIgniter Forums
Array member in function name OR How can a variable change its value depending on how I use it after it's declared? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Array member in function name OR How can a variable change its value depending on how I use it after it's declared? (/thread-54565.html)



Array member in function name OR How can a variable change its value depending on how I use it after it's declared? - El Forum - 09-14-2012

[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>&lt;?php print_r($instance); ?&gt;</pre>

  &lt;?php $name = $instance['taxon_field']; ?&gt;
  &lt;?php echo 'Value of the $name variable is: ' . $name; ?&gt;

&lt;?php endforeach; ?&gt;
&lt;?php endforeach; ?&gt;
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:
&lt;?php $name(); ?&gt;
(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:
&lt;?php foreach ($content['id'] as $key1 => $taxon): ?&gt;
&lt;?php foreach ($taxon as $key2 => $instance): ?&gt;

  <pre>&lt;?php print_r($instance); ?&gt;</pre>

  &lt;?php $name = $instance['taxon_field']; ?&gt;
  &lt;?php echo $name(); ?&gt;

&lt;?php endforeach; ?&gt;
&lt;?php endforeach; ?&gt;
(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.