将不同的多个数组合并为单个数组

时间:2022-10-06 16:51:56

Thing getting more difficult now. I have a query result like this:

现在越来越难了。我有这样的查询结果:

Result = array(

结果=数组(

A = array(
        [a]=>'1',
        [b] =>'2',
     );

B = array(
        [a] =>'1',
        [b] =>'3',
     );

C = array(
        [a] =>'1',
        [b] =>'4',
     );

**and more...**

);

So how can I merge them into one array like this:

那么如何将它们合并到一个数组中,如下所示:

Result = array(

结果=数组(

     Z = array(

        [a] =>'1',
        [b] => array(

               [0] =>'2',
               [1] =>'3',
               [2] =>'4',
        );

     );

);

Thanks you and sorry for my poor presentation.

谢谢你,抱歉我的演讲很糟糕。

3 个解决方案

#1


2  

You can look into array_merge function in PHP Manual

您可以在PHP Manual中查看array_merge函数

#2


1  

I don't know why you keep asking the same question but here's my answer to your problem.

我不知道为什么你一直在问同样的问题,但这是我对你的问题的答案。

$result = array_merge_recursive($a, $b, $c);
foreach($result as $k => $v) $result[$k] = array_unique($v);
print_r($result);

#3


1  

If you plan to only have primitive values in the keys (strings, numbers etc.) this will work:

如果您计划只在键(字符串,数字等)中使用原始值,这将起作用:

<?php

function array_combine_keys($a, $b) {
    $c = $a;
    foreach ($b as $k=>$v) {
        // Value can be only primitive, not an array itself
        if (isset($a[$k]) && $a[$k] !== $v) {
            $c[$k] = array($a[$k], $v);
        } else {
            $c[$k] = $v;
        }
    }
    return $c;
}

$a = array('a' => '1', 'b' => '2');
$b = array('a' => '1', 'b' => '3', 'c' => '5');

var_dump(array_combine_keys($a, $b));

?>

array(3) {
  ["a"]=>
  string(1) "1"
  ["b"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "3"
  }
  ["c"]=>
  string(1) "5"
}

A more versatile way is to allow arrays in the values as well, and to ensure the keys are always arrays irrespectively - this means we won't need extra logic to check if the key is an array or isn't an array when traversing the result. The (+) is the union of the two arrays.

一种更通用的方法是允许数组中的数组,并确保键始终是数组 - 这意味着我们不需要额外的逻辑来检查键是数组还是遍历时不是数组结果。 (+)是两个数组的并集。

<?php

function array_combine_keys($a, $b) {
    $c = $a;
    foreach ($b as $k=>$v) {
        if (!is_array($v)) {
            $v = array($v);
        }
        if (isset($a[$k])) {
            $av = $a[$k];
            if (!is_array($av)) {
                $av = array($av);
            }
            $c[$k] = $av + $v;
        } else {
            $c[$k] = $v;
        }
    }
    return $c;
}

$a = array('a' => '1', 'b' => array('2', '4'));
$b = array('a' => '1', 'b' => array('3'), 'c' => '5');

var_dump(array_combine_keys($a, $b));

?>

array(3) {
  ["a"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "4"
  }
  ["c"]=>
  array(1) {
    [0]=>
    string(1) "5"
  }
}

#1


2  

You can look into array_merge function in PHP Manual

您可以在PHP Manual中查看array_merge函数

#2


1  

I don't know why you keep asking the same question but here's my answer to your problem.

我不知道为什么你一直在问同样的问题,但这是我对你的问题的答案。

$result = array_merge_recursive($a, $b, $c);
foreach($result as $k => $v) $result[$k] = array_unique($v);
print_r($result);

#3


1  

If you plan to only have primitive values in the keys (strings, numbers etc.) this will work:

如果您计划只在键(字符串,数字等)中使用原始值,这将起作用:

<?php

function array_combine_keys($a, $b) {
    $c = $a;
    foreach ($b as $k=>$v) {
        // Value can be only primitive, not an array itself
        if (isset($a[$k]) && $a[$k] !== $v) {
            $c[$k] = array($a[$k], $v);
        } else {
            $c[$k] = $v;
        }
    }
    return $c;
}

$a = array('a' => '1', 'b' => '2');
$b = array('a' => '1', 'b' => '3', 'c' => '5');

var_dump(array_combine_keys($a, $b));

?>

array(3) {
  ["a"]=>
  string(1) "1"
  ["b"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "3"
  }
  ["c"]=>
  string(1) "5"
}

A more versatile way is to allow arrays in the values as well, and to ensure the keys are always arrays irrespectively - this means we won't need extra logic to check if the key is an array or isn't an array when traversing the result. The (+) is the union of the two arrays.

一种更通用的方法是允许数组中的数组,并确保键始终是数组 - 这意味着我们不需要额外的逻辑来检查键是数组还是遍历时不是数组结果。 (+)是两个数组的并集。

<?php

function array_combine_keys($a, $b) {
    $c = $a;
    foreach ($b as $k=>$v) {
        if (!is_array($v)) {
            $v = array($v);
        }
        if (isset($a[$k])) {
            $av = $a[$k];
            if (!is_array($av)) {
                $av = array($av);
            }
            $c[$k] = $av + $v;
        } else {
            $c[$k] = $v;
        }
    }
    return $c;
}

$a = array('a' => '1', 'b' => array('2', '4'));
$b = array('a' => '1', 'b' => array('3'), 'c' => '5');

var_dump(array_combine_keys($a, $b));

?>

array(3) {
  ["a"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "4"
  }
  ["c"]=>
  array(1) {
    [0]=>
    string(1) "5"
  }
}