【问题标题】:How can I merge one array with values into an array with stdClass objects?如何将一个带有值的数组合并到一个带有 stdClass 对象的数组中?
【发布时间】:2015-05-15 10:47:04
【问题描述】:

我有两个数组,正在寻找合并它们的方法。标准array_merge() 功能不起作用。

你知道没有 foreach 迭代的好解决方案吗?

我的第一个数组:

Array
(
    [0] => stdClass Object
        (
            [field_value] => Green
            [count] => 
        )

    [1] => stdClass Object
        (
            [field_value] => Yellow
            [count] => 
        )
)

我的第二个数组:

Array
(
    [0] => 2
    [1] => 7
)

因此我想得到:*

Array
(
    [0] => stdClass Object
        (
            [field_value] => Green
            [count] => 2
        )

    [1] => stdClass Object
        (
            [field_value] => Yellow
            [count] => 7
        )
)

【问题讨论】:

  • 请将您的尝试添加到您的问题中。
  • 我试过array_merge函数。
  • @user889349 然后将您的尝试添加到您的问题中并展示您的工作/努力

标签: php arrays multidimensional-array array-merge


【解决方案1】:

这应该适合你:

只需使用array_map() 循环遍历两个数组,然后将数组一的参数作为引用传递。然后,您可以简单地将值分配给 count 属性。

<?php

    array_map(function(&$v1, $v2){
        $v1->count = $v2;
    }, $arr1, $arr2);

    print_r($arr1);

?>

输出:

Array
(
    [0] => stdClass Object
        (
            [field_value] => Green
            [count] => 2
        )

    [1] => stdClass Object
        (
            [field_value] => Yellow
            [count] => 7
        )

)

【讨论】:

【解决方案2】:
 [akshay@localhost tmp]$ cat test.php
 <?php

  $first_array = array( 
            (object)array("field_value"=>"green","count"=>null),
            (object)array("field_value"=>"yellow","count"=>null)
             );

  $second_array = array(2,7);


  function simple_merge($arr1, $arr2)
  {
    return array_map(function($a,$b){ $a->count = $b; return $a; },$arr1,$arr2);
  }

  print_r($first_array);
  print_r($second_array);
  print_r(simple_merge($first_array,$second_array));

 ?>

输出

 [akshay@localhost tmp]$ php test.php
 Array
 (
     [0] => stdClass Object
         (
             [field_value] => green
             [count] => 
         )

     [1] => stdClass Object
         (
             [field_value] => yellow
             [count] => 
         )

 )
 Array
 (
     [0] => 2
     [1] => 7
 )
 Array
 (
     [0] => stdClass Object
         (
             [field_value] => green
             [count] => 2
         )

     [1] => stdClass Object
         (
             [field_value] => yellow
             [count] => 7
         )

 )

【讨论】:

    【解决方案3】:

    很简单

    代码:

     $i = 0;
        foreach($firstarrays as $firstarr)
        {
            $firstarr['count'] = $secondarray[$i];
            $i++;
        }
    

    【讨论】:

      【解决方案4】:

      另一种选择:

      $a1 = Array(
          (object) Array('field_value' => 'Green', 'count' => null),
          (object) Array('field_value' => 'Yellow', 'count' => null)
      );
      
      $a2 = Array(2, 7);
      
      for ($i=0; $i<sizeof($a1); $i++) {
          $a1[$i]->count=$a2[$i];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-22
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多