Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question is followed by incredible buggy code, but here is a fixed code, which return the result you need:</p> <pre><code>&lt;!-- language: lang-php --&gt; &lt;?php // How do I pass an array key to a function to pull up the right key's data? $var['TEST1'] = Array ( 'Description' =&gt; 'This is a Description', 'Version' =&gt; '1.11', 'Fields' =&gt; Array( 'ID' =&gt; array( 'type' =&gt; 'int', 'length' =&gt;'11', 'misc' =&gt;'auto_increment' ), 'DATA' =&gt; array( 'type' =&gt; 'varchar', 'length' =&gt; '255' ) ) ); $var['TEST2'] = Array ( 'Description' =&gt; 'This is the 2nd Description', 'Version' =&gt; '2.1', 'Fields' =&gt; Array( 'ID' =&gt; array( 'type' =&gt; 'int', 'length' =&gt; '11', 'misc' =&gt;'auto_increment' ), 'DATA' =&gt; array( 'type' =&gt; 'varchar', 'length' =&gt; '255' ) ) ); function buildStructure($obj) { global $var; $output = ''; // $primaryKey = $VAR[$obj]['primary key']; // Primary key is not // defined in original array!!! foreach ($var[$obj]['Fields'] as $name =&gt; $tag){ $type = $tag['type']; $length = $tag['length']; $default = (array_key_exists('default', $tag)) ? $tag['default'] : ''; $description = (array_key_exists('description', $tag)) ? $tag['description'] : ''; $length = (isset($length)) ? "({$length})" : ''; $default = ($default == NULL ) ? "NULL" : $default; $output .= "`{$name}` {$type}{$length} DEFAULT {$default} COMMENT `{$description}`, "; } return $output; } $obj = 'TEST1'; echo buildStructure($obj); // output is: // `ID` int(11) DEFAULT NULL COMMENT ``, `DATA` varchar(255) DEFAULT NULL COMMENT ``, ?&gt; </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