【问题标题】:When push objects to array in php, all objects are same在php中将对象推送到数组时,所有对象都是相同的
【发布时间】:2012-05-11 06:38:56
【问题描述】:

我想将许多对象推入一个数组中

每个对象都有不同的价值

但是当我将它们推入数组时

它们的所有值都相同

如何解决这个问题?

$sql="select password, mail from account";
$result=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($result))
{
    $o->pw=$row['password'];
    $o->mail=$row['mail'];
    array_push($arr, $o);
}
echo json_encode($arr);

【问题讨论】:

  • "all values of them are same" 这是什么意思?你能显示数组的print_r() 吗?
  • $o 是什么?这是从哪里来的?
  • 我不确定,但您可能必须创建对象的新实例。 $o 一直指向同一个对象。
  • 如果您不使用来自array_push() 函数的返回值,使用$arr[] = $o 表示法会更快。
  • 如果有人想知道,这里已经回答了这个问题:stackoverflow.com/questions/5625308/…

标签: php arrays json object


【解决方案1】:
$sql="select password, mail from account"; 
$result=mysql_query($sql);
$data = [];
while($row=mysql_fetch_assoc($result)){
array_push($data, ['password' => $row['password'],
           'mail' => $row['mail'],]);
}
header('Content-Type: application/json');
$encode_data = json_encode($data);
echo $encode_data;

【讨论】:

    【解决方案2】:

    我认为您需要为循环的每次迭代都实例化一个新对象。现在,对于循环的每次迭代,只有一个 $o 被写入,这就是为什么它们看起来都具有相同的值:它们是相同的。

    试试这个:

    while($row=mysql_fetch_assoc($result))
    {
        $o = new stdClass();
        $o->pw=$row['password'];
        $o->mail=$row['mail'];
        array_push($arr, $o);
    }
    

    【讨论】:

      【解决方案3】:

      这是因为对象作为引用被添加到数组中。数组中的每个元素都是对一个对象的引用,同一个对象。

      你没有声明$o,所以当你第一次声明$o->pw时,PHP会为你创建一个对象。当它这样做时,它会在循环范围之外创建它,因此循环的每次迭代都引用相同的$o

      您需要在每次循环迭代时声明$o

      while($row=mysql_fetch_assoc($result))
      {
          $o = new stdClass;
          $o->pw = $row['password'];
          $o->mail = $row['mail'];
          array_push($arr, $o);
      }
      

      【讨论】:

        【解决方案4】:

        你真的不需要在php中使用太多push,你可以使用空括号来附加它。不确定它是否有所作为,但我发现括号更容易。此外,此代码中似乎没有定义 O,也没有在循环中重置。这可能就是问题所在,尽管我对你的问题总体上不是很清楚。祝你好运

        $sql="select password, mail from account";
        $result=mysql_query($sql);
        $arr=array();
        while($row=mysql_fetch_assoc($result))
        {
            //define/reset o here
            $o->pw=$row['password'];
            $o->mail=$row['mail'];
            $arr[] = $o;
        }
        echo json_encode($arr);
        

        【讨论】:

          【解决方案5】:

          那是因为你每次都将同一个对象推入数组中。

          您应该在每次迭代中推送一个新对象。例如,如果$ostdClass 对象,则在循环内使用$o = new stdClass

          while($row=mysql_fetch_assoc($result))
          {
              $o = new stdClass;
              $o->pw=$row['password'];
              $o->mail=$row['mail'];
              array_push($arr, $o);
          }
          

          您也可以使用mysql_fetch_object,这可能更合适:

          while($o=mysql_fetch_object($result))
          {
              array_push($arr, $o);
          }
          

          上述对象的属性将根据您的 SQL 查询列命名,因此为了达到相同的效果,您还需要将查询更改为 select password AS pw, mail from account

          最后,另一种选择是每次clone 对象 -- 尽管其他选择几乎总是更可取:

          while($row=mysql_fetch_assoc($result))
          {
              $o = clone $o;
              $o->pw=$row['password'];
              $o->mail=$row['mail'];
              array_push($arr, $o);
          }
          

          【讨论】:

            【解决方案6】:

            尝试先声明$o(在while循环内):

            $o = new stdClass;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-12-25
              • 1970-01-01
              • 2015-04-20
              • 2019-04-08
              • 1970-01-01
              相关资源
              最近更新 更多