How to combine multiple arrays with identical keys into a single array?
I am new to php. I need some help. I had a array as
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..
That is not possible. A PHP array cannot have two identical keys.
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] =>
)
)
)
Have a look at PHP's array_merge()
-function.
精彩评论