NiTx Posted April 30, 2013 Share Posted April 30, 2013 Hey guys, I'm trying to run a while loop for an array that includes an ID value in each row. I'm trying to temporarily disable that value while I perform a foreach loop, then prepend the value back into the array after the foreach loop is finished. function array_unshift_assoc(&$arr, $key, $val) { $arr = array_reverse($arr, true); $arr[$key] = $val; $arr = array_reverse($arr, true); } So far I've come up with this function (which works), but I feel like there may be an easier way? Rest of my code is below. Thoughts? while($row = mysql_fetch_assoc($get_specs)) { //Set the phones id to a temporary variable. $tempid = $row['id']; //Unset the id so that we can perform the foreach loop without //it interfering with the function. unset($row['id']); foreach($row as $spec => $value) { get_spec_rating($spec, $value); echo $rating . "<br/><br/>"; } //Unshift the id value back into the array array_unshift_assoc($row, "id", $tempid); } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/ Share on other sites More sharing options...
Solution requinix Posted April 30, 2013 Solution Share Posted April 30, 2013 Or just not output anything if $spec=="id". Quote Link to comment https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/#findComment-1427216 Share on other sites More sharing options...
NiTx Posted April 30, 2013 Author Share Posted April 30, 2013 Or just not output anything if $spec=="id". Thanks requinix, I thought of this, but if my function passes the key ID andits value, I get an error. I could try to alter my function to suit, but it would be a pain in the ass to say the least. Still, thanks for the tip Quote Link to comment https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/#findComment-1427217 Share on other sites More sharing options...
Philip Posted April 30, 2013 Share Posted April 30, 2013 I thought of this, but if my function passes the key ID andits value, I get an error. I could try to alter my function to suit, but it would be a pain in the ass to say the least. You don't need to edit the function. as requinix said: while($row = mysql_fetch_assoc($get_specs)) { foreach($row as $spec => $value) { if($spec == 'id') continue; get_spec_rating($spec, $value); echo $rating . "<br/><br/>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/#findComment-1427218 Share on other sites More sharing options...
NiTx Posted April 30, 2013 Author Share Posted April 30, 2013 Never used 'continue' before so thanks! Added to my repertoire Again, thank you both! Quote Link to comment https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/#findComment-1427220 Share on other sites More sharing options...
Barand Posted April 30, 2013 Share Posted April 30, 2013 Here's an easier way to temporarily remove the id $arr = array ( array( 'id' => 1, 'k2' => 'a', 'k3' => 'b' ), array( 'id' => 2, 'k2' => 'c', 'k3' => 'd' ), array( 'id' => 3, 'k2' => 'e', 'k3' => 'f' ) ); foreach ($arr as $row) { foreach (array_slice($row,1) as $k=>$v) { echo "$k | $v<br>"; } print_r($row); // row is unchanged echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/#findComment-1427239 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.