Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamicly set a array variable?
    text
    copied!<p>I have a problem with this code:</p> <pre><code>$a['i'] = 1; $b = '$a[\'i\']'; echo $$b; </code></pre> <p>It display an error: </p> <blockquote> <p>Notice: Undefined variable: $a['i'] in test.php on line 6</p> </blockquote> <p>Is it possible to create dynamic array variable?</p> <p>Thanks for your time.</p> <p>EDIT: In my example I am trying to edit an multidimensional array. There is a problem when I try to add data to my array (JSON). I don't have fixed dimension of array, it my be 2 or more dimension (I am building a model for Web form, and I want to add invalid value to JSON).</p> <p>Now in one of the methods of Web form object I have code which checks repopulation object to add invalid value if needed.</p> <p>I can not just add a value to JSON array, I need to edit it on multidimensional level.</p> <p>For now I came up on solution to dynamically generate variable name and, then, edit it. If someone have solution it will be appreciated.</p> <pre><code>private $form = array( 'form_contact'=&gt;array( 'attr'=&gt;array('tag'=&gt;'FORM', 'method'=&gt;'post'), 'elem'=&gt;array( 'fs_contact'=&gt;array( 'attr'=&gt;array('legend'=&gt;'Kontakt', 'tag'=&gt;'FSET'), 'elem'=&gt;array( 'name'=&gt;array( 'attr'=&gt;array('SPAN'=&gt;'Ime i prezime', 'title'=&gt;'Unesite Vaše ime i prezime', 'tag'=&gt;'INPUT', 'type'=&gt;'text'), 'validat'=&gt;array('req'=&gt;'noscript', 'length'=&gt;255), 'invalid'=&gt;true), // Holds info that this is invalid 'www'=&gt;array( 'attr'=&gt;array('SPAN'=&gt;'Web sajt', 'title'=&gt;'Unesite Vaš sajt', 'tag'=&gt;'INPUT', 'type'=&gt;'text'), 'validat'=&gt;array('length'=&gt;255)), 'email'=&gt;array( 'attr'=&gt;array('SPAN'=&gt;'E-mail', 'title'=&gt;'Unesite Vaš email', 'tag'=&gt;'INPUT', 'type'=&gt;'text'), 'validat'=&gt;array('req'=&gt;'email', 'length'=&gt;255)), 'message'=&gt;array( 'attr'=&gt;array('SPAN'=&gt;'Poruka', 'cols'=&gt;'60', 'rows'=&gt;'5', 'title'=&gt;'Unesite Vašu poruku', 'tag'=&gt;'TEXTA', 'value'=&gt;'nesto'), 'validat'=&gt;array('req'=&gt;'all')), 'submit_new_contact_form'=&gt;array( 'attr'=&gt;array('tag'=&gt;'INPUT', 'type'=&gt;'submit', 'value'=&gt;'Pošalji poruku!')) ))// FS end )) // Form end );// Array end </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