在php中为数组对象添加一个新的std类

时间:2015-04-24 12:43:52

标签: php codeigniter

我使用var_dump得到以下数组的类/对象;

$all_stores = array (size=4)
        0 => 
        object(stdClass)[29]
          public 'id' => string '1' (length=1)
          public 'store_name' => string 'asdasd' (length=6)
          public 'desc' => string ' sdasdasd' (length=9)
          public 'profile' => 
            object(stdClass)[32]
              public 'id' => string '1' (length=1)
              public 'fname' => string 'Daniel' (length=6)
              public 'lname' => string 'adenew' (length=6)
              public 'gender' => null
              public 'zipcode' => string '63011' (length=5)

和另一个std类

$media =
    object(stdClass)[34]
      public 'id' => string '16' (length=2)
      public 'title' => null
   (length=37)
      public 'type' => string 'store_image' (length=11)
      public 'profile_id' => string '1' (length=1)

我真正想要的是将这两个类合并为std / Objects。 我需要将媒体类的每个成员添加为profile变量中的$all_stores之类的关键或变量。

预期输出如下

0 => 
        object(stdClass)[29]
          public 'id' => string '1' (length=1)
          public 'store_name' => string 'asdasd' (length=6)
          public 'desc' => string ' sdasdasd' (length=9)
          public 'profile' => 
            object(stdClass)[32]
              public 'id' => string '1' (length=1)
              public 'fname' => string 'Daniel' (length=6)
              public 'lname' => string 'adenew' (length=6)
              public 'gender' => null
              public 'zipcode' => string '63011' (length=5)
          public 'media' => 
            object(stdClass)[34]
             public 'id' => string '16' (length=2)
             public 'title' => null

          public 'zipcode' => string '63011' (length=5)

三江源

1 个答案:

答案 0 :(得分:0)

获取您需要的媒体。我打算做一个例子:

$medias = $this->media_model->getMyMedia();

之后你所要做的就是这样设置:

$all_stores->medias = $medias;

或者因为你的$ all_stores是一个数组:

$all_stores[0]->medias = $medias;

被修改

    $all_stores= $this->with('xxx')->get_all();

     //get all medias file associated with the stores


 foreach ($all_stores as $key => $value) {
    $all_stores[$key]->media = $this->media->get_by('store_id',$value->id);
   }
        var_dump($all_stores);exit;