合并两个数组

时间:2012-04-26 10:03:50

标签: php

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

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"
   }

是否有任何功能可以做或者我需要写循环?

5 个答案:

答案 0 :(得分:0)

第一个数组,称之为$ images

Seconds数组将其称为$ objects

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

var_dump($objects); //Check the result

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为没有特定的PHP功能,所以请尝试以下方法:

foreach ($array2 as $i => &$item) {
  $item->img = $array2[$i];
}

答案 3 :(得分: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)

答案 4 :(得分: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>";