简体   繁体   中英

PHP: Incorrect value after assignment

I have really no idea what's happening. The story: I use PDO for a SELECT statement on a database.

$sql = "SELECT a,b,c,performance,points,compare 
        FROM normvalues 
        WHERE x=:x AND y=1 AND z=:z";
$stmt = $GLOBALS['PDO']->prepare($sql);
$stmt->bindParam(":x",$x);
$stmt->bindParam(":z",$z);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);

So that's fine and it is working. When I var_dump the $res variable I get something like:

array(6) {
    ["a"]=> string(2) "44"
    ["b"]=> string(4) "1176"
    ["c"]=> string(4) "1166"
    ["performance"]=> string(4) "50.1"
    ["points"]=> string(1) "1"
    ["compare"]=> string(2) "-1"
  }
  [1]=>
  array(6) {
    ["a"]=> string(2) "57"
    ["b"]=> string(4) "1176"
    ["c"]=> string(4) "1166"
    ["performance"]=> string(4) "47.7"
    ["points"]=> string(1) "2"
    ["compare"]=> string(2) "-1"
  }
  [2]=>
  array(6) {
    ["a"]=> string(2) "70"
    ["b"]=> string(4) "1176"
    ["c"]=> string(4) "1166"
    ["performance"]=> string(4) "44.7"
    ["points"]=> string(1) "3"
    ["compare"]=> string(2) "-1"
  }
    ...

That's also okay. But I have to sort the result another way. So I am doing:

foreach($res as $e){
      $this->normTable[$e['a']][$e['points']]=$e['performance'];
      $this->normTable[$e['a']]['compare']=$e['compare'];
    }

And now I am completely lost. By assigning $e['performance'] I get wrong values. Actually this should be the performance values.

 [1176]=>
  array(4) {
    [1]=> string(2) "50"
    ["compare"]=> string(2) "-1"
    [2]=> string(2) "48"
    [3]=> string(2) "45"
  }

I already checked the values in the database and they are correct. By doing doubleval() I'd get the right values, but the problem is that not every value is a double but also integer or string. I also tried to typecast with (string) but it's the same result. I have no explanation.

Update:

It's a very big project and I just tried to minimize it as possible and to make my problem as clear as possible. But now I have figured out something new: I do an 'echo()' of my first variable in the normTable during the loop:

foreach($res as $e){
 $this->normTable[$e['a']][$e['points']]=$e['performance'];
 echo "a:".$e['a']." pt: ".$e['points']." perf: ".$e['performance']."-".$this->normTable[1176][1]."\n";
 $this->normTable[$e['a']]['compare']=$e['compare'];
}

and the value is changing from '50.1' to '50'. Still can't figure out the reason. Is there a size limitaion of arrays in PHP?

UPDATE 2 and a big SORRY!

As I said, it is a big project. So the table I read out, has some values for some attributes twice or more. Actually such a case should not happen. That's why the answer is simple: It became 50 because 50 was assigned. I'm so sorry for having waisted your time. But I totally excluded this case and since I am also coding in C, my first thought was: memory leak - clear case!

Thanks for your help.

Meh, I don't have 50 reputation; can't comment only answer.

If you replace a with be in your loop, you should get your expected result (given your data sample). Ie:

foreach($res as $e){
  $this->normTable[$e['b']][$e['points']]=$e['performance'];
  $this->normTable[$e['b']]['compare']=$e['compare'];
}

But I'm not sure this really solves your problem. YMMV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM