0

我必须数组。这是第一个:

array(3) 
{
  [0]=> string(10) "image1.jpg"
  [1]=> string(10) "image2.jpg"
  [2]=> string(10) "image3.jpg"
}

第二个:

array(3)
{
[0]=> object(stdClass)#22 (4) 
   { 
    ["id"]=> string(1) "1" 
    ["name"]=> string(6) "Name 1"
    ["file"]=> string(15) "f1335421531.zip"
    ["desc"]=> string(6) " "
   }
[1]=> object(stdClass)#23 (4) 
   {
    ["id"]=> string(1) "2"
    ["name"]=> string(6) "Name 2"
    ["file"]=> string(15) "f1335421552.zip"
    ["desc"]=> string(6) " "
   }
[2]=> object(stdClass)#24 (4)
   {
    ["id"]=> string(1) "3"
    ["name"]=> string(6) "Name 3"
    ["file"]=> string(15) "f1335421588.zip"
    ["desc"]=> string(6) " " 
   }
 }

如何将这些数组合并为一个包含以下项目:

[0]=> object(stdClass)#22 (4) 
   { 
    ["id"]=> string(1) "1" 
    ["name"]=> string(6) "Name 1"
    ["file"]=> string(15) "f1335421531.zip"
    ["desc"]=> string(6) " "
    ["img"]=> string(10) "image1.jpg"
   }

等等

是否有任何功能可以做到这一点,或者我可能需要编写循环?

4

5 回答 5

0

第一个数组,称之为 $images

秒数组称之为 $objects

for($i=0; $i<count($objects); $i++){
    $object['img'] = $images[i];
}

var_dump($objects); //Check the result
于 2012-04-26T10:08:20.527 回答
0

您需要将第二个数组的每个元素转换为当前为对象类型的数组类型,然后使用 array_merge 函数对其进行迭代,您可以实现您想要的。

于 2012-04-26T10:09:56.503 回答
0

我认为没有特定的 php 函数,所以请尝试以下操作:

foreach ($array2 as $i => &$item) {
  $item->img = $array2[$i];
}
于 2012-04-26T10:12:09.950 回答
0

尝试

$std1 = new stdClass ();
$std1->id = "1";
$std1->name = "Name 1";
$std1->file = "f1335421531.zip";
$std1->desc = "";

$std2 = new stdClass ();
$std2->id = "2";
$std2->name = "Name 2";
$std2->file = "f1335421552.zip";
$std2->desc = "";

$std3 = new stdClass ();
$std3->id = "3";
$std3->name = "Name 3";
$std3->file = "f1335421588.zip";
$std3->desc = "";

$obj = array (
        $std1,
        $std2,
        $std3 
);
$image = array (
        "image1.jpg",
        "image2.jpg",
        "image3.jpg" 
);

for($i = 0; $i < count ( $obj ); $i ++) {
    $obj [$i]->img = $image [$i];
}

var_dump ( $obj );

输出

array
  0 => 
    object(stdClass)[1]
      public 'id' => string '1' (length=1)
      public 'name' => string 'Name 1' (length=6)
      public 'file' => string 'f1335421531.zip' (length=15)
      public 'desc' => string '' (length=0)
      public 'img' => string 'image1.jpg' (length=10)
  1 => 
    object(stdClass)[2]
      public 'id' => string '2' (length=1)
      public 'name' => string 'Name 2' (length=6)
      public 'file' => string 'f1335421552.zip' (length=15)
      public 'desc' => string '' (length=0)
      public 'img' => string 'image2.jpg' (length=10)
  2 => 
    object(stdClass)[3]
      public 'id' => string '3' (length=1)
      public 'name' => string 'Name 3' (length=6)
      public 'file' => string 'f1335421588.zip' (length=15)
      public 'desc' => string '' (length=0)
      public 'img' => string 'image3.jpg' (length=10)
于 2012-04-26T10:15:38.677 回答
0
Try this:
$arr=array("image1.jpg","image2.jpg","image3.jpg");
$obj= array((object)array("id"=>"1","name"=>"name1","file"=>"asdb1.zip","desc"=>" "),
            (object)array("id"=>"2","name"=>"name2","file"=>"asdb2.zip","desc"=>" "),
            (object)array("id"=>"3","name"=>"name3","file"=>"asdb3.zip","desc"=>" ")
  );
for($i=0;$i<count($obj);$i++){
    $newObj=(array)$obj[$i];
    $newObj['img']=$arr[$i];
    $newArr[]=(object)$newObj;  
}
echo "<pre>";var_dump($newArr);echo "</pre>";       
于 2012-04-26T10:53:26.110 回答