views:

121

answers:

3

hi to all. 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..

A: 

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

elusive
those are not different arrays to use merge function..those are again in a array. so i want that to be a single array..thx for ur reply
bsrreddy
+7  A: 

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

codaddict
i am sorry. i changed the query. so that i got different keys. but the values might be equal some time. can we form that in a single array. Array ( [_] => Array ( [0] => [1] => ) [123_aug] => Array ( [0] => 123 [1] => ) [1234_aug] => Array ( [0] => 1234 [1] => ) ) Array ( [_] => Array ( [0] => [1] => ) [12345_aug] => Array ( [0] => 123 [1] => ) [123456_aug] => Array ( [0] => 1234 [1] => ) ) how can ii form this..
bsrreddy
@bsrreddy: You sill have the key `_` common in both the arrays.
codaddict
oh..i am sorry.i didnt observe that and i just given that as example. neglect that.there is no matter of same keys..actually thats a resultset i got through looping a query..hope u understand .
bsrreddy
+1  A: 

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] =>
                )

        )

)
M42