twome Posted April 25, 2006 Share Posted April 25, 2006 I have an item that I get from the database, it's a string separated by commas. To get the individual pieces I use the explode command. I would like the array index to be the same value as the exploded pieces. How can I do that? I imagine I would have a for each loop? Any suggestions?Here's what I have so far:[code]$query = "SELECT name, address, type FROM member m INNER JOIN profile p ON ";$query .= "m.memberID = p.memberID WHERE m.memberID = '".$sessUserID."'"; $dbResults = mysql_query($query);$result = mysql_fetch_array($dbResults); $pieces = explode(",", $result['type']);print_r ($pieces);[/code][u]Output[/u]Array ( [0] => 1 [1] => 5 [2] => 10 [3] => 12 )[u]What I want the array to look like is this[/u]:Array ( [1] => 1 [5] => 5 [10] => 10 [12] => 12 )[u]Ultimately, I have a group of checkboxes on a form[/u]:<input name="type[]" type="checkbox" value="1" /><input name="type[]" type="checkbox" value="2" /><input name="type[]" type="checkbox" value="3" />........<input name="type[]" type="checkbox" value="14" />Pretty much, I need to match the values in the array with the checkboxes -- if there is a match then display that checkbox as "checked".So according to the array results in the above example the checked check boxes would be:<input name="type[]" type="checkbox" value="1" checked="checked" /><input name="type[]" type="checkbox" value="5" checked="checked" /><input name="type[]" type="checkbox" value="10" checked="checked" /><input name="type[]" type="checkbox" value="12" checked="checked" /> Quote Link to comment https://forums.phpfreaks.com/topic/8342-using-arrays-and-explode-in-php/ Share on other sites More sharing options...
KrisNz Posted April 25, 2006 Share Posted April 25, 2006 [code]#i took this function of php.net/array_combine#if you're using php5 you can just call array_combinefunction array_combine_emulated( $keys, $vals ) { $keys = array_values( (array) $keys ); $vals = array_values( (array) $vals ); $n = max( count( $keys ), count( $vals ) ); $r = array(); for( $i=0; $i<$n; $i++ ) { $r[ $keys[ $i ] ] = $vals[ $i ]; } return $r;}$testArray = array("10","12","13","34","15");$keys = array_values(($testArray));$newArray = array_combine_emulated($keys,$testArray); print_r($newArray); # prints Array ( [10] => 10 [12] => 12 [13] => 13 [34] => 34 [15] => 15 ) [/code] Quote Link to comment https://forums.phpfreaks.com/topic/8342-using-arrays-and-explode-in-php/#findComment-30427 Share on other sites More sharing options...
Barand Posted April 25, 2006 Share Posted April 25, 2006 try[code]$query = "SELECT name, address, type FROM member m INNER JOIN profile p ON ";$query .= "m.memberID = p.memberID WHERE m.memberID = '".$sessUserID."'"; $dbResults = mysql_query($query);$result = mysql_fetch_array($dbResults); $pieces = explode(",", $result['type']);for ($i=1; $i <=14; $i++) { $chk = in_array($i, $pieces) ? 'checked' : ''; echo "<input name='type[]' type='checkbox' value='$i' $chk /> $i<br>";}[/code]Setting the checkbox value to $i instead of '1' means you can now update the type column by[code]$pieces = join (',', $_POST['type']);$sql = "UPDATE member SET `type` = '$pieces' WHERE memberID = '$sessUserID' ";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8342-using-arrays-and-explode-in-php/#findComment-30497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.