多维数组上的数组合并 [英] Array merge on multidimensional array

查看:70
本文介绍了多维数组上的数组合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是盲人,还是在SO的任何地方都找不到此问题. 昨天我在合并数组时遇到问题,可以在SO的帮助下解决.今天,我再次遇到合并数组的问题,但这一次是多维数组.

Either I'm blind or I can't find this problem anywhere here on SO. Yesterday I had a problem with merging arrays, which I could fix with the help of SO. Today I have, again, a problem with merging arrays, but this time it's with multidimensional Arrays.

我有一个数组$usergroup['groups']和一个数组$usergroup['lang']

I have an array $usergroup['groups'] and an array $usergroup['lang']

$usergroup['groups']看起来像这样:

Array
(
    [0] => Usergroup_Model Object
        (
            [id] => 1
            [deleted] => 0
        )

    [1] => Usergroup_Model Object
        (
            [id] => 2
            [deleted] => 0
        )

    [2] => Usergroup_Model Object
        (
            [id] => 3
            [deleted] => 0
        )

)

$usergroup['lang']看起来像这样:

Array
(
    [0] => Usergroup_Model Object
        (
            [id] => 
            [id_usergroup] => 1
            [name] => Administratoren
            [id_lang] => 1
        )

    [1] => Usergroup_Model Object
        (
            [id] => 
            [id_usergroup] => 2
            [name] => Benutzer
            [id_lang] => 1
        )

    [2] => Usergroup_Model Object
        (
            [id] => 
            [id_usergroup] => 3
            [name] => Gäste
            [id_lang] => 1
        )

)

我希望我的合并数组看起来像这样:

I want my merged array to look like this:

Array
(
    [0] => Usergroup_Model Object
        (
            [id] => 1
            [id_usergroup] => 1
            [name] => Administratoren
            [id_lang] => 1
            [deleted] => 0
        )

    [1] => Usergroup_Model Object
        (
            [id] => 2
            [id_usergroup] => 2
            [name] => Benutzer
            [id_lang] => 1
            [deleted] => 0
        )

    [2] => Usergroup_Model Object
        (
            [id] => 3
            [id_usergroup] => 3
            [name] => Gäste
            [id_lang] => 1
            [deleted] => 0
        )

)

我尝试了什么?

我已经尝试了几个PHP的合并函数(array_merge()array_merge_recursive()),得到的最接近结果是第二个Array(['lang'])覆盖了第一个Array(['groups']).为了解决这个问题,我尝试删除了lang数组(始终为id)上的空值.但这并不能解决问题.此刻的代码如下所示:

I've tried several merging functions (array_merge() and array_merge_recursive()) of PHP, the closest result I got was, that the second Array (['lang']) overwrote the first Array (['groups']). To fix that, I tried to remove the empty values on the lang Array (which is always id). But that does not fix it. The code - at the moment - looks like this:

public static function getAll()
{
  $usergroup['groups'] = self::find();
  $usergroup['lang'] = self::findInTable(array(
    'id_lang' => Language_Model::getDefaultLanguage()
  ), self::dbTranslationTable);
  foreach ($usergroup as $ug) {
    $ug = array_filter($ug, function($val) {
      return $val != '';
    });
  }
  return array_merge($ug);
}

return命令上的array_merge()似乎根本不做任何事情,因此我可能没有正确收集数据,或者我弄乱了Arrays(忘记添加[],否则我不会这样做).不知道...).我有点想念这里的树林.

The array_merge() on the return command doesn't seem to do anything at all, so I'm probably not gathering the data correctly or I mess something up with the Arrays (forgetting to add [], or I don't know...). I kinda miss the forest for the trees here.

关于我可以朝哪个方向发展的任何建议?

Any suggestions in which direction I could go?

使用PédeLeão提供的代码,我能够解决此问题.我的功能现在看起来像这样:

With the code provided by Pé de Leão I was able to solve the problem. My function now looks like this:

public static function getAll()
{
  $usergroup['groups'] = self::find();
  $usergroup['lang'] = self::findInTable(array(
    'id_lang' => Language_Model::getDefaultLanguage()
  ), self::dbTranslationTable);
  $out = array();
  foreach ($usergroup['groups'] as $key => $value) {
    $out[] = (object) array_merge((array) $usergroup['lang'][$key], (array) $value);
  }
  return $out;
}

结果正是我想要的!

推荐答案

$out = array();
foreach ($arr1 as $key => $value){
    $out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)

这篇关于多维数组上的数组合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆