0

我很迷茫,在下面的代码中:

while($row = mysqli_fetch_object($result)){
        $similar_games[$row->game_id]['id'][] = $row->id;
        $similar_games[$row->game_id]['name'][] = $row->name;
        $similar_games[$row->game_id][$type] = 0;
    }
        foreach($similar_games as $originalGameKey => $originalGame){
            //Can be a case where union gets unset, and not populated.  scares ksort();
            $union = array();
            $similar_values = array();

            $union = $similar_concepts; 

            foreach($originalGame['id'] as $similarGameKey => $similarGame){
                if(!isset($union[$similarGame])){
                    $union[] = $similarGame;
                }
            }
            foreach($originalGame['id'] as $similarGameKey => $similarGame){
                if(isset($union[$similarGame])){
                    $similar_values[] = $similarGame;
                }
            }
            $similar_games[$originalGameKey]["union"] = $similar_values;
       }

在这条线上$similar_values[] = $similarGame;我试图得到$row->name而不是$row->id

但我不知道如何使基于 ['id'] 的 foreach 访问 ['name'] 值。

如果这太令人困惑,我可以尝试澄清,但我自己在这里遇到了麻烦。

4

1 回答 1

0

我只是改变了:

foreach($originalGame['id'] as $similarGameKey => $similarGame){
                if(isset($union[$similarGame])){
                    $similar_values[] = $similarGame;
                }
            }
            $similar_games[$originalGameKey]["union"] = $similar_values;

至:

for($i = 0; $i<count($originalGame['id']);$i++){
                if(isset($union[$originalGame['id'][$i]])){
                    $similar_values[] = $originalGame['name'][$i];
                }
            }
            $similar_games[$originalGameKey]["union"] = $similar_values;
于 2012-07-22T21:54:09.923 回答