1

I have following array:

$DS = array(
    'name' => 'kim',
    'star' => 'singh'

);

I want adding new array in it. this array: 'relation' => '100','original' => '1'

Finally, it will look like:

$DS = array(
    'name' => 'kim',
    'star' => 'singh',
    'relation' => '100',
    'original' => '1'

);

My tried as:

$DS = array(
    'name' => 'kim',
    'star' => 'singh'

);
array_push($DS, array(
    'relation' => '100',
    'original' => '1'
));

echo '<pre>';
print_r($DS);

Output:

Array
(
    [name] => kim
    [star] => singh
    [0] => Array
        (
            [relation] => 100
            [original] => 1
        )

)

Demo:http://codepad.viper-7.com/ouzBxN

How can done it, please give me example.

2
  • $DS['relation'] = '100'; DS['original'] = '1'; Commented Feb 14, 2014 at 18:46
  • for u... If I am correct... array push for next row.,,. not for another column Commented Feb 14, 2014 at 18:50

2 Answers 2

5

Use array_merge()

$new_array = array_merge($DS, array(
    'relation' => '100',
    'original' => '1'
));
Sign up to request clarification or add additional context in comments.

Comments

0

This works Fine:

$array1 = array(
        'name' => 'kim',
        'star' => 'singh'

    );

    $array2 = array(
        'relation' => '100',
        'original' => '1'

    );
    $array3 = $array1+$array2;

    print_r($array3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.