0

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..

5
  • please use print_r, var_dump or better yet var_export when providing variable dumps. Commented Sep 17, 2010 at 8:08
  • 1
    that's impossible. when you call "123_" element, which one it should be? Commented Sep 17, 2010 at 8:09
  • 1
    What do you use the array for? What does it represent? Do you want to detect duplicates and merge these or do you want to prevent duplicates by giving them unique keys? Commented Sep 17, 2010 at 8:10
  • You practically give the answer yourself: "there woul be duplicate keys and values". If it's duplicate it wouldn't be a key anymore, would it? Commented Sep 17, 2010 at 8:13
  • 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..but there are same values with different keys..can u explain how can i get all these as a single array..thx in advance. Commented Sep 17, 2010 at 10:22

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

3 Comments

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: You sill have the key _ common in both the arrays.
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 .
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] =>
                )

        )

)

Comments

0

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.