如何制作两个数组对象的单个数组?

时间:2015-08-24 12:01:27

标签: php codeigniter

如何制作两个数组对象的单个数组数组对象 我有以下两个对象数组

如何制作两个数组对象的单个数组数组对象 我有以下两个对象数组

   Array
    (
        [0] => stdClass Object
            (
                [id] => 25
                [authorityId] => 2
                [grantVillage] => test
                [agency] => test
                [certificate] => Panding
                [amount] => 50000
                [startDate] => 2015-12-08
                [endDate] => 2015-12-08
                [grantArea] => test
                [added_date] => 0000-00-00
            )

        [1] => stdClass Object
            (
                [id] => 26
                [authorityId] => 2
                [grantVillage] => testing2
                [agency] => testing
                [certificate] => Verified
                [amount] => 50000
                [startDate] => 1970-01-01
                [endDate] => 1970-01-01
                [grantArea] => test
                [added_date] => 0000-00-00
            )

    )
    Array
    (
        [0] => stdClass Object
            (
                [name] => kandi Area
            )

        [1] => stdClass Object
            (
                [name] => kandi Area
            )

    )

我想成功:

 Array
    (
        [0] => stdClass Object
            (
                [name] => kandi Area
                [id] => 25
                [authorityId] => 2
                [grantVillage] => test
                [agency] => test
                [certificate] => Panding
                [amount] => 50000
                [startDate] => 2015-12-08
                [endDate] => 2015-12-08
                [grantArea] => test
                [added_date] => 0000-00-00
            )

        [1] => stdClass Object
            (
                [name] => kandi Area
                [id] => 26
                [authorityId] => 2
                [grantVillage] => testing2
                [agency] => testing
                [certificate] => Verified
                [amount] => 50000
                [startDate] => 1970-01-01
                [endDate] => 1970-01-01
                [grantArea] => test
                [added_date] => 0000-00-00
            )

    )

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

您只需使用array_merge

$combinedArray = array_merge($array1, $array2);

编辑:误解了您的问题,以下内容可行,因为您只需要遍历数组2并将这些值添加到数组1中:

<?php      
  $array1 = array (
    0 => array (
      "id" => "25",
      "authorityId" => "2",
    ),
    1 => array (
      "id" => "26",
      "authorityId" => "2",
    )
  );

  $array2 = array (
    0 => array ( 
      "name" => "kandi Area"
    ),
    1 => array ( 
      "name" => "kandi Area"
    )
  );

  foreach ($array2 as $array2Key => $array2ValuesArray) {
    # This works for serialized arrays:
    // $array1[$array2Key]['name'] = $array2ValuesArray['name']; 
    # However this does not relate to the OP which means that this will work for stdClass Objects:
    $array1[$array2Key]->name = $array2ValuesArray->name;
  }

  echo '<pre>';
  print_r($array1);
  echo '</pre>';
?>

编辑:这会产生以下输出:

array
(
  [0] => stdClass Object
  (
    [id] => 25
    [authorityId] => 2
    [name] => kandi Area
  ),
  [1] => stdClass Object
  (
    [id] => 26
    [authorityId] => 2
    [name] => kandi Area
  )
)

答案 1 :(得分:0)

您可以合并两个数组对象,并可以通过以下问题获取数组对象:

How to merge two arrays of object in PHP

获得合并数组对象后。您可以通过以下代码片段将数组对象(std类)转换为数组:

$array = json_decode(json_encode($ArrayObject),true);