Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert string in MySQL table field to array in PHP
    text
    copied!<p><strong>Problem:</strong></p> <p>I have a field in my MySQL table with the following value:</p> <p>9, 10, 11, 12, 13, 14, 15, 16, 26, 27, 28, 29, 30, 31, 32</p> <p><strong>I use PHP to put the value of this field in the variable: $row['Exclude'];</strong></p> <p>The problem is that I am using a function called rand_except() that looks as following:</p> <pre><code>function rand_except($min, $max, $except) { //first sort array values sort($except, SORT_NUMERIC); //calculate average gap between except-values $except_count = count($except); $avg_gap = ($max - $min + 1 - $except_count) / ($except_count + 1); if ($avg_gap &lt;= 0) return false; //now add min and max to $except, so all gaps between $except-values can be calculated array_unshift($except, $min - 1); array_push($except, $max + 1); $except_count += 2; //iterate through all values of except. If gap between 2 values is higher than average gap, // create random in this gap for ($i = 1; $i &lt; $except_count; $i++) if ($except[$i] - $except[$i - 1] - 1 &gt;= $avg_gap) return mt_rand($except[$i - 1] + 1, $except[$i] - 1); return false; } </code></pre> <p>In order for this to work it needs to be like this:</p> <pre><code>$exclude = array(9, 10, 11, 12, 13, 14, 15, 16, 26, 27, 28, 29, 30, 31, 32); $_SESSION['experimentversion'] = rand_except(1, 32, $exclude); </code></pre> <p><strong>Question:</strong></p> <p>How can I take the database field $row['Exclude'] and transform it into an array so it will work with the function?</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