使用for循环在PHP中创建一个对象数组

时间:2013-11-08 12:38:16

标签: php arrays oop

我正在尝试从mysql数据中获取数据,从中创建对象。在这里,我写了一些代码。我从数据库中获取了东西的细节。现在,对于每个结果,我想要创建一个最终与数组输出合并的对象。这个$output数组最终输出为json。

我收到错误:

array_merge(): Argument #2 is not an array

如何在PHP中创建对象数组?

public function getDetails()    //This is a class function
{
    $query = "SELECT * FROM `thing` WHERE `id` = :thingId";
    $stmt = $dbh->prepare ( $query );
    $stmt->bindParam ( ":thingId" , $_GET['thingId']  );
    $stmt->execute ( );
    $rslt  =  $stmt->fetch ( );
    $thingName  = $rslt['name'];
    $thingOwnerId = $rslt['userId'];
    $thingDescription = $rslt['thingDescription'];

    // Getting the thing owner details
    $query = "SELECT * from `user` WHERE ( `id` = :id ) ";    
    $stmt = $dbh->prepare( $query );
    $stmt->bindParam ( ":id" , $thingOwnerId );
    $stmt->execute(  );
    $rslt = $stmt->fetch ( );
    $thingOwnerName = $rslt['firstName']." ".$rslt['lastName'];
}

$query = "SELECT * FROM `things` ; ";
$s = $dbh->prepare($query);
$s->execute();
$r = $s->fetchAll();
foreach($r as $r1)
{
    $newThing = new Thingy($r1['id']);
    $newThing->getDetails();
    $output =  array_merge($output,$newThing);
}

$output2 = json_encode($output);
header('content-type: application/json');
echo $output2;

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

$output = [];
foreach($r as $r1)
{
    $newThing = new Thingy($r1['id']);
    $newThing->getDetails();
    $output[] = $newThing;
}

答案 1 :(得分:1)

您可以在此处节省一些工作,并使用PDO::FETCH_CLASS作为$fetch_style。基本上,它允许您指定自定义类,您可以使用查询中每行的匹配返回值来填充这些属性。请注意方法签名中的第一个参数:

  

public array PDOStatement :: fetchAll([int $ fetch_style [,mixed $ fetch_argument [,array $ ctor_args = array()]]])

PDOStatement::fetchAll() documentation

中的示例#4所示
<?php
class fruit {
    public $name;
    public $colour;
}

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_CLASS, "fruit");
var_dump($result);

希望这会有所帮助:)

答案 2 :(得分:0)

我看到一些可能导致问题的事情。

首先,可能是您的问题的原因是array_merge只接受array类型的参数。 See the documentation。但是,您可以将$newThing的类型转换为数组,或者更简洁地只需手动添加as suggested in this other answer

其次,您的方法getDetails()既不返回任何内容,也不是修改对象内容的类方法。它需要是一个或另一个;现在它运行,结果不会永久存储在任何地方。既然你已经表明它是一个类方法,那么它应该是这样的:

public function getDetails()    //This is a class function
{
    $query = "SELECT * FROM `thing` WHERE `id` = :thingId";
    $stmt = $dbh->prepare ( $query );
    $stmt->bindParam ( ":thingId" , $_GET['thingId']  );
    $stmt->execute ( );
    $rslt  =  $stmt->fetch ( );
    $this->thingName  = $rslt['name'];                    // Class variable
    $this->thingOwnerId = $rslt['userId'];                // Class variable
    $this->thingDescription = $rslt['thingDescription'];  // Class variable

    // Getting the thing owner details
    $query = "SELECT * from `user` WHERE ( `id` = :id ) ";    
    $stmt = $dbh->prepare( $query );
    $stmt->bindParam ( ":id" , $thingOwnerId );
    $stmt->execute(  );
    $rslt = $stmt->fetch ( );
    $this->thingOwnerName = $rslt['firstName']." ".$rslt['lastName']; // Class variable
}