Jump to content

Auto replace array values ?


caspergmf

Recommended Posts

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).

Link to comment
https://forums.phpfreaks.com/topic/209732-auto-replace-array-values/
Share on other sites

<?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

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.

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'];
}
?>

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

<?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'];
}
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.