caspergmf Posted August 4, 2010 Share Posted August 4, 2010 Hello, I am a newbie PHP hobbyist. I mess with php strictly for my own amusement. Up until now I have been able to Google my way to a answer to my questions but I'm lost on this one. I'm not really sure what to even search for. I have a few different mysql queries that returns the following arrays. qid15=37,37,37,37,37,37,37,37,37,37,37,38,38,39,39,41 qid17=47,47,47,47,47,48,49,49,49,50,50,50,51,51 qid18=52,52,52,52,52,52,52,52,54,54,55,55,55,56,56,56 qid19=57,57,57,57,58,59,59,61,61 What I would like to do is replace or create a new array for each result with the lowest value as 1 the next lowest as 2 then 3 and 4. My new array would look something like this oldqid15=37,37,37,37,37,37,38,38,39,39,41 newqid15=1,1,1,1,1,1,2,2,3,3,4 I sure would appreciate any ideas on this. I'm thinking there is probably a simple solution and before I waste anymore time I'll stop and ask for directions(as my wife would suggest). Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/ Share on other sites More sharing options...
trq Posted August 4, 2010 Share Posted August 4, 2010 Can we see some code and what your query actually returns? MySql can't store arrays, so its hard to tell from your description what you actually have. Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1094877 Share on other sites More sharing options...
caspergmf Posted August 4, 2010 Author Share Posted August 4, 2010 <?php include("connect/login.php"); $query= "SELECT AID FROM answer WHERE (QID = 18) AND UID IN (SELECT UID FROM answer WHERE Answer = 33)"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) echo $row['AID']; ?> 52525252525252525454555555565656 Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1094916 Share on other sites More sharing options...
trq Posted August 4, 2010 Share Posted August 4, 2010 Ok, it's still pretty unclear to me what your after. My new array would look something like this oldqid15=37,37,37,37,37,37,38,38,39,39,41 newqid15=1,1,1,1,1,1,2,2,3,3,4 1,2,3 & 4 aren't even in your original string. Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1094923 Share on other sites More sharing options...
caspergmf Posted August 5, 2010 Author Share Posted August 5, 2010 I may be wording this all wrong being a rookie and all. In my mysql query “SELECT AID FROM answer WHERE (QID = 18)” the QID has four different possibilities 15,17,18,19 which will return four different results. My goal is to take the lowest numeric value in each returned results and make it a value of one. The next lowest value in the result would become a value of two and so on. I would then like to put those results into a variable(array). I’ve been able to somewhat accomplish this by adding this to the code I posted earlier, (echoed for testing purpose). if ($row[AID]=="52") echo "1"; if ($row[AID]=="54") echo "2"; if ($row[AID]=="55") echo "3"; if ($row[AID]=="56") echo "4"; Original return 52525252525252525454555555565656 After “if” statement 1111111122333444 This will work with the QID=18 but will not for the other three possibilities I guess I would have to write a “if” statement for each. Hope this makes it a little clearer what I’m trying to do. Is this a good way of doing this or is there a more efficient way. Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1095374 Share on other sites More sharing options...
sasa Posted August 5, 2010 Share Posted August 5, 2010 try <?php include("connect/login.php"); $query= "SELECT AID FROM answer WHERE (QID = 18) AND UID IN (SELECT UID FROM answer WHERE Answer = 33) ORDER BY AID ASC"; $result = mysql_query($query) or die(mysql_error()); $i = 1; $start = false; $tmp = -1; while($row = mysql_fetch_array($result)){ if($start and $row['AID'] > $tmp){ $i++; $tmp = $row['AID']; } $start = true; echo $i; // echo $row['AID']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1095444 Share on other sites More sharing options...
caspergmf Posted August 6, 2010 Author Share Posted August 6, 2010 The code in the last post is exactly what I was looking for thank you very much sasa. It all seems so simple when you actually know what your doing(unlike myself). One slight problem though, the first value always returns a 1 and the rest of the same value return a 2 then everything else follows sequence as it should. Is there something I need to tweak? 52,52,52,52,52,52,52,52,54,54,55,55,55,56,56,56 1,2,2,2,2,2,2,2,3,3,4,4,4,5,5,5 Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1095829 Share on other sites More sharing options...
sasa Posted August 6, 2010 Share Posted August 6, 2010 <?php include("connect/login.php"); $query= "SELECT AID FROM answer WHERE (QID = 18) AND UID IN (SELECT UID FROM answer WHERE Answer = 33) ORDER BY AID ASC"; $result = mysql_query($query) or die(mysql_error()); $i = 0; $tmp = -1; while($row = mysql_fetch_array($result)){ if($row['AID'] > $tmp){ $i++; $tmp = $row['AID']; } echo $i; // echo $row['AID']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/#findComment-1095839 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.