简体   繁体   中英

Merging two arrays

I have to arrays. Here is first:

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

And second one:

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

How can I merge these arrays into one with following items:

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

etc.

Is there any function to do it or maybe I need to write loop?

First array, call it $images

Seconds array call it $objects

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

var_dump($objects); //Check the result

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

I don't think there is a specific php function for this, so try the the following:

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

try

$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 );

Output

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)
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>";       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM