Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

php - Array merge on multidimensional array

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.

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

$usergroup['groups'] looks like this:

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
        )

)

And $usergroup['lang'] looks like this:

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
        )

)

What have I tried?

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);
}

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?

Edit: 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;
}

And the result is exactly how I wanted it!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$out = array();
foreach ($arr1 as $key => $value){
    $out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...