【问题标题】:How to make single array of two array object?如何制作两个数组对象的单个数组?
【发布时间】:2015-08-24 12:01:27
【问题描述】:

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

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

   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
            )

    )

任何帮助将不胜感激。在此先感谢。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

你可以合并两个数组对象,按照这个问题可以得到一个数组对象:

How to merge two arrays of object in PHP

一旦你得到合并数组对象。您可以通过此代码 sn-p 将数组对象(std 类)转换为数组:

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

【讨论】:

    【解决方案2】:

    您可以简单地使用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
      )
    )
    

    【讨论】:

    • 我不想合并请再次检查我的问题。
    • 它不工作消息:不能使用 stdClass 类型的对象作为数组
    • @rakeshstellen 查看我对输出的编辑,因为它经过了 100% 的测试并且可以使用 PHP 5.5.28。
    • 在您的回答中,您在数组中有数组,但在我的问题中,数组中有 stdClass 对象。
    • 是的,它可以工作,但对于数组内的数组而不是数组内的 stdClass 对象,请再次检查我的问题
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2017-02-11
    • 2018-06-09
    • 2020-03-29
    • 1970-01-01
    相关资源
    最近更新 更多