将一个数组与数组的对象合并


Merge one array with object of array

我有一个对象有一个像

这样的数组
object(ElggObject)[1023]
  protected 'attributes' => 
    array
      'guid' => string '39344' (length=5)
      'type' => string 'object' (length=6)
      'subtype' => string '25' (length=2)
      'owner_guid' => string '39136' (length=5)
      'container_guid' => string '39136' (length=5)
      'site_guid' => string '1' (length=1)
      'access_id' => string '2' (length=1)
      'time_created' => string '1359536708' (length=10)
      'time_updated' => string '1359536708' (length=10)
      'last_action' => string '1359536708' (length=10)
      'enabled' => string 'yes' (length=3)
      'tables_split' => int 2
      'tables_loaded' => int 2
      'title' => string 'Community developer 5' (length=21)
      'description' => string '<p>$entity = get_entity($entity_guid);</p>' (length=42)
  protected 'url_override' => null
  protected 'icon_override' => null
  protected 'temp_metadata' => 
    array
      empty.........

上面的一个只有一个对象。另一个数组的对象数组如下所示。所以我需要合并两件事…

array
  0 => 
    object(ElggObject)[1031]
      protected 'attributes' => 
        array
          'guid' => string '35116' (length=5)
          'type' => string 'object' (length=6)
          'subtype' => string '25' (length=2)
          'owner_guid' => string '2' (length=1)
          'container_guid' => string '2' (length=1)
          'site_guid' => string '1' (length=1)
          'access_id' => string '2' (length=1)
          'time_created' => string '1322222868' (length=10)
          'time_updated' => string '1402048391' (length=10)
          'last_action' => string '1322222868' (length=10)
          'enabled' => string 'yes' (length=3)
          'tables_split' => int 2
          'tables_loaded' => int 2
          'title' => string 'Developer for Ramco Systems' (length=27)
          'description' => string '<ul>
  1 => 
    object(ElggObject)[1034]
      protected 'attributes' => 
        array
          'guid' => string '39339' (length=5)
          'type' => string 'object' (length=6)
          'subtype' => string '25' (length=2)
          'owner_guid' => string '39136' (length=5)
          'container_guid' => string '39136' (length=5)
          'site_guid' => string '1' (length=1)
          'access_id' => string '2' (length=1)
          'time_created' => string '1359535318' (length=10)
          'time_updated' => string '1402032776' (length=10)
          'last_action' => string '1359535318' (length=10)
          'enabled' => string 'yes' (length=3)
          'tables_split' => int 2
          'tables_loaded' => int 2
          'title' => string 'Community developer 2' (length=21)
          'description' => string '<p>Job description jkjkhjkhk</p>' (length=32)
  2 => 
    object(ElggObject)[1037]
      protected 'attributes' => 
        array
          'guid' => string '39342' (length=5)
          'type' => string 'object' (length=6)
          'subtype' => string '25' (length=2)
          'owner_guid' => string '39136' (length=5)
          'container_guid' => string '39136' (length=5)
          'site_guid' => string '1' (length=1)
          'access_id' => string '2' (length=1)
          'time_created' => string '1359536322' (length=10)
          'time_updated' => string '1401962732' (length=10)
          'last_action' => string '1359536322' (length=10)
          'enabled' => string 'yes' (length=3)
          'tables_split' => int 2
          'tables_loaded' => int 2
          'title' => string 'Community developer 4' (length=21)

那么如何合并两件事…我试过array_merge,但对我不起作用…

任何建议. .

试试这个:

$result = (object)array_merge((array)$obj1, (array)$obj2);

PHP中可以使用array_merge_recursive函数对多维数组进行合并。

例如,

$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array("color1" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);

重要的是Key应该是不同的。

如果key是唯一的,值将从具有相同key的不同数组中合并。

编辑:

$secondArray[] = $firstArrayObj;

试试这个。