Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable variables to call programmatically-created arrays
    text
    copied!<p>I am creating a series of arrays from template tags in a content management system, and outputting the titles of these arrays as arrays labeled with variables:</p> <pre><code>&lt;Loop&gt; $&lt;GeneratedArrayName1&gt; = array( "foo" =&gt; "bar" ); $&lt;GeneratedArrayName2&gt; = array( "foo" =&gt; "bar" ); &lt;/Loop&gt; </code></pre> <p>I'm also generating another array of possible GeneratedArrayNames and sorting it by count. I'm looping through this array to get the GeneratedArrayNames so I can selectively show them. After I sort, I want to pull and display only a few of the many arrays I have with GeneratedArrayNames. I'm doing this by positioning the pointer at the top of the master array and getting the name:</p> <pre><code>reset($ArrayNames); $firstArray = current($ArrayNames); //outputs GeneratedArrayName1 </code></pre> <p>Then I went to pull the GeneratedArrayName by getting the Variable variable, which gives me an error:</p> <pre><code>print_r(${$firstArray}); // outputs Undefined variable: GeneratedArrayName1 </code></pre> <p>But when I hardcode, I get the correct data: </p> <pre><code>print_r($GeneratedArrayName1); // outputs the array </code></pre> <p>Where am I going wrong?</p> <p><em>EDIT</em></p> <p>I am getting <code>$firstArray</code> by this loop:</p> <pre><code>$count = 0; foreach($ArrayNames as $ArrayCount =&gt; $ArrayName) { $count++; echo "$ArrayName" . ' - ' . "$ArrayCount" . '&lt;br&gt;'; if ($count &gt;= 3) { break; } } //from here I proceed to reset($ArrayNames) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload