Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble Dollar Sign - Assign Array key=>value
    text
    copied!<p>I would like to create 3 different arrays which can change depending on which fields are pulled from some mysql tables.</p> <p>This is the code I have:</p> <pre><code> // Gather db rows with query $compQuery['subCats'] = mysql_query("SELECT id,name,category FROM subcategories ORDER BY id DESC"); $compQuery['cats'] = mysql_query("SELECT id,name FROM categories ORDER BY id DESC"); $compQuery['mans'] = mysql_query("SELECT id,name,link FROM manufacturers ORDER BY id DESC"); // Place rows into arrays foreach($compQuery as $name=&gt;$query){ $name = array(); while($data = mysql_fetch_array($compQuery[$name])){ foreach($data as $key=&gt;$value){ $$name[$key] = $value; } } } </code></pre> <p>What I would expect this to produce is 3 arrays: subCats, cats and mans each with the value pairs. Instead of finding the $$name variable and assigning [$key] = $value to the array it is taking a single letter out of the $$name variable and assigning the $value to this new variable.</p> <p>For example, if $name = 'subCats', $key = '3', and value = 'machine tools' </p> <p>It would create the variable $C using the 3rd letter in subCats:</p> <pre><code>$C = 'machine tools' </code></pre> <p>What it should do is create:</p> <pre><code>$subCats[3] = 'machine tools'; </code></pre> <p>Now, obviously there are easier ways to create separate arrays for each query, and I have modified my code to a lazier/easier version. However, I am still curious as to how I would go about assigning a key value pair to a double dollar sign variable.</p> <p>Any help would be greatly appreciated! I have the simple code working now, but would like to implement this new code to be more dynamic and allow for more queries as needed without hassle.</p> <p>Thanks!</p> <p>Josh</p>
 

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