0

I have any array

$num_list = array(42=>'0',44=>'0',46=>'0',48=>'0',50=>'0',52=>'0',54=>'0',56=>'0',58=>'0',60=>'0');

and I want to change specific values as I go through a loop

    while(list($pq, $oin) = mysql_fetch_row($result2)) {
        $num_list[$oin] = $pq;
    }

So I want to change like 58 to 403 rather then 0.

However I always end up getting just the last change and non of the earlier ones. So it always ends up being something like 0,0,0,0,0,0,0,0,0,403 rather then 14,19,0,24,603,249,0,0,0,403

How can I do this so it doesn't overwrite it?

Thanks

3
  • 1
    What are the values that $pq and $oin can have? Commented May 21, 2010 at 8:05
  • Sorry, let me explain a bit more. $oin must be a value in the array (42,44,46,48,50,etcetc) $pq can be any WHOLE number. Commented May 21, 2010 at 8:07
  • So, you wish to overwrite it only with values bigger than "0" or just one value (you select) at a time? Commented May 21, 2010 at 8:22

3 Answers 3

1

Well, you explicititly coded that each entry should be replaced with the values from the database (even with "0"). You could replace the values on non-zero-values only:

while(list($pq, $oin) = mysql_fetch_row($result2)) {
    if ($pq !== "0") $num_list[$oin] = $pq;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I don't get you more clear, i thought your asking this only. Check this

while(list($pq, $oin) = mysql_fetch_row($result2)) {
       if($oin==58) {
           $num_list[$oin] = $pq;
       }
    }

Comments

1

In my simulated tests (although You are very scarce with information), Your code works well and produces the result that You want. Check the second query parameter, that You put into array - namely $pg, thats what You should get there 0,0,0,0,0...403 OR Other thing might be that Your $oin numbers are not present in $num_list keys. I tested Your code with mysqli driver though, but resource extraction fetch_row is the same.

Bear in mind one more thing - if Your query record number is bigger than $numlist array, and $oin numbers are not unique, Your $numlist may be easily overwritten by the folowing data, also $numlist may get a lot more additional unwanted elements.

Always try to provide the wider context of Your problem, there could be many ways to solve that and help would arrive sooner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.