Note that there are some explanatory texts on larger screens.

plurals
  1. POphp: foreach() how to assign to two variables
    primarykey
    data
    text
    <pre><code>function example() { foreach ($choices as $key =&gt; $choice) { # \__ both should run parallel foreach ($vtitles as $keystwo =&gt; $vtitle) { # / $options .= '&lt;option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected .'&gt;'. check_plain($choice) .'&lt;/option&gt;'; } // end of vtitle } // end of choice return $options; } </code></pre> <p><strong>Answers to some of the below questions and what I am trying to achieve.</strong></p> <ol> <li>Array <code>$choices</code> is not numerically indexed.</li> <li>Array <code>$vtitle</code> is numerically indexed.</li> <li>They won't be shorter than each other as I have code which will take care of this before this code runs.</li> <li>I am trying to return <code>$options</code> variable. The issue is that <code>$choices[0]</code> and <code>$vtitle[0]</code> should be used only once. Hope I was able to express my problem. </li> <li>I do not want to go through the <code>$vtitles</code> array once for each value in <code>$choices</code>.</li> </ol> <p>@hakre: thanks I have nearly solved it with your help.</p> <p>I am getting an error for variable <code>$vtitle</code>:</p> <pre><code>InvalidArgumentException: Passed variable is not an array or object, using empty array instead in ArrayIterator-&gt;__construct() (line 35 of /home/vishal/Dropbox/sites/chatter/sites /all/themes/kt_vusers/template.php). </code></pre> <p>I am sure its an array this is the output using <code>print_r</code></p> <pre><code>Array ( [0] =&gt; vishalkh [1] =&gt; newandold ) </code></pre> <p>What might be going wrong ?</p> <p>The below worked for me , thank you hakre</p> <pre><code>while ( (list($key1, $value1) = each($array1)) &amp;&amp; (list($key2, $value2) = each($array2)) ) { printf("%s =&gt; %s, %s =&gt; %s \n", $key1, $value1, $key2, $value2); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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