如何将具有相同键的多个数组组合成一个数组?

时间:2022-03-11 12:56:36

I am new to php. I need some help. I had a array as

我是php的新手。我需要一些帮助。我有一个阵列

Array ( [_] => Array ( [0] => [1] => )
        [123_] => Array ( [0] => 123 [1] => ) 
        [1234_] => Array ( [0] => 1234 [1] => )
      )
Array ( [_] => Array ( [0] => [1] => )
        [12345_] => Array ( [0] => 12345 [1] => ) 
        [1234_] => Array ( [0] => 1234 [1] => )
      )

so..whats my problem is i want an array with all these keys and values as

所以..我的问题是我想要一个包含所有这些键和值的数组

Array ( [_] => Array ( [0] => [1] => )
        [123_] => Array ( [0] => 123 [1] => ) 
        [1234_] => Array ( [0] => 1234 [1] => )
        [_] => Array ( [0] => [1] => )
        [12345_] => Array ( [0] => 12345 [1] => ) 
        [1234_] => Array ( [0] => 1234 [1] => )
  )

there would be duplicate keys and values.. but I want all of them as a array.. any help plz..

会有重复的键和值..但我想将它们全部作为一个数组..任何帮助PLZ ..

3 个解决方案

#1


7  

That is not possible. A PHP array cannot have two identical keys.

这是不可能的。 PHP数组不能有两个相同的键。

#2


1  

As the others said, it's impossible to have a single array with duplicate keys. But you can build an array of array :

正如其他人所说,拥有重复键的单个阵列是不可能的。但是你可以构建一个数组数组:

<?php
$arr1 = array( '_' => Array ( '0' => '', '1' => ''),
             '123_' => Array ( '0' => 123, '1' => ''), 
             '1234_' => Array ( '0' => 1234, '1' => '')
);
$arr2 = array ( '_' => Array ( '0' => '', '1' => ''),
               '12345_' => Array ( '0' => 12345, '1' => ''), 
               '1234_' => Array ( '0' => 1234, '1' => '')
);
$result = array();
foreach( $arr1 as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arr2 as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
print_r($result);
?>

Ouput:

输出继电器:

Array
(
    [0] => Array
        (
            [key] => _
            [value] => Array
                (
                    [0] =>
                    [1] =>
                )

        )

    [1] => Array
        (
            [key] => 123_
            [value] => Array
                (
                    [0] => 123
                    [1] =>
                )

        )

    [2] => Array
        (
            [key] => 1234_
            [value] => Array
                (
                    [0] => 1234
                    [1] =>
                )

        )

    [3] => Array
        (
            [key] => _
            [value] => Array
                (
                    [0] =>
                    [1] =>
                )

        )

    [4] => Array
        (
            [key] => 12345_
            [value] => Array
                (
                    [0] => 12345
                    [1] =>
                )

        )

    [5] => Array
        (
            [key] => 1234_
            [value] => Array
                (
                    [0] => 1234
                    [1] =>
                )

        )

)

#3


0  

Have a look at PHP's array_merge()-function.

看看PHP的array_merge() - 函数。

#1


7  

That is not possible. A PHP array cannot have two identical keys.

这是不可能的。 PHP数组不能有两个相同的键。

#2


1  

As the others said, it's impossible to have a single array with duplicate keys. But you can build an array of array :

正如其他人所说,拥有重复键的单个阵列是不可能的。但是你可以构建一个数组数组:

<?php
$arr1 = array( '_' => Array ( '0' => '', '1' => ''),
             '123_' => Array ( '0' => 123, '1' => ''), 
             '1234_' => Array ( '0' => 1234, '1' => '')
);
$arr2 = array ( '_' => Array ( '0' => '', '1' => ''),
               '12345_' => Array ( '0' => 12345, '1' => ''), 
               '1234_' => Array ( '0' => 1234, '1' => '')
);
$result = array();
foreach( $arr1 as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arr2 as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
print_r($result);
?>

Ouput:

输出继电器:

Array
(
    [0] => Array
        (
            [key] => _
            [value] => Array
                (
                    [0] =>
                    [1] =>
                )

        )

    [1] => Array
        (
            [key] => 123_
            [value] => Array
                (
                    [0] => 123
                    [1] =>
                )

        )

    [2] => Array
        (
            [key] => 1234_
            [value] => Array
                (
                    [0] => 1234
                    [1] =>
                )

        )

    [3] => Array
        (
            [key] => _
            [value] => Array
                (
                    [0] =>
                    [1] =>
                )

        )

    [4] => Array
        (
            [key] => 12345_
            [value] => Array
                (
                    [0] => 12345
                    [1] =>
                )

        )

    [5] => Array
        (
            [key] => 1234_
            [value] => Array
                (
                    [0] => 1234
                    [1] =>
                )

        )

)

#3


0  

Have a look at PHP's array_merge()-function.

看看PHP的array_merge() - 函数。