1

Sorry if this title maybe seem misleading, I really didn't know how to ask this question.

So basicly I have an array like this

    $array=
[
        'Drinks'=>[
              'Water'=> [
                 'ID'   => '4',
                 'Name'   => 'Clean-water',
                 'Value'  => '1',
                 'Made'   => 'Acient',
                 'Stackable' => true
               ],

               'Wine'=> [
                   'ID'   => '5',
                  'Name'   => 'Soff',
                  'Value'  => '5',
                  'Made'   => 'Acient',
                  'Stackable' => true
                ],

                'Vodka'=> [
                   'ID'   => '6',
                   'Name'   => 'Laudur',
                   'Value'  => '7',
                   'Made'   => 'Acient',
                   'Stackable' => true
                 ]



            ]
        ];

I want to add another array to 'Vodka' like with values like this:

                'Vodka'=> [
                   'ID'   => '6',
                   'Name'   => 'Laudur',
                   'Value'  => '7',
                   'Made'   => 'Acient',
                   'Stackable' => true
                 ],[
                    'ID'   => '7',
                   'Name'   => 'Test',
                   'Value'  => '9',
                   'Made'   => 'New',
                   'Stackable' => true
                           ]

But when I var_dump I wont get this array to 'Vodka' array, it will create [0] array.

array (size=2)
  'Weapons' => 
    array (size=3)
      'Sword' => 
        array (size=5)
          'ID' => string '1' (length=1)
          'Name' => string 'Lurker' (length=6)
          'Value' => string '12' (length=2)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean false
      'Shield' => 
        array (size=5)
          'ID' => string '2' (length=1)
          'Name' => string 'Obi' (length=3)
          'Value' => string '22' (length=2)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean false
      'Warhammer' => 
        array (size=5)
          'ID' => string '3' (length=1)
          'Name' => string 'Clotch' (length=6)
          'Value' => string '124' (length=3)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean false
  'Drinks' => 
    array (size=4)
      'Water' => 
        array (size=5)
          'ID' => string '4' (length=1)
          'Name' => string 'Clean-water' (length=11)
          'Value' => string '1' (length=1)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean true
      'Wine' => 
        array (size=5)
          'ID' => string '5' (length=1)
          'Name' => string 'Soff' (length=4)
          'Value' => string '5' (length=1)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean true
      'Vodka' => 
        array (size=5)
          'ID' => string '6' (length=1)
          'Name' => string 'Laudur' (length=6)
          'Value' => string '7' (length=1)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean true
      0 => 
        array (size=5)
          'ID' => string '7' (length=1)
          'Name' => string 'Viru' (length=4)
          'Value' => string '8' (length=1)
          'Made' => string 'Acient' (length=6)
          'Stackable' => boolean true

How can I fix this, because I really want to add this to 'Vodka' array, but I dont want to create another 'Drinks' array?

2
  • Yes, I saw it. Thanks you and user3020875 for helping me! Commented Sep 6, 2017 at 15:37
  • Glad to hear that Commented Sep 6, 2017 at 15:37

2 Answers 2

4

You need to do is:-

$array['Drinks']['Vodka']=[
                $array['Drinks']['Vodka'], [
                'ID'   => '7',
               'Name'   => 'Test',
               'Value'  => '9',
               'Made'   => 'New',
               'Stackable' => true
            ]
        ];

print_r($array);

Output:-https://eval.in/856440

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

Comments

3
$add = [
   'ID'   => '7',
   'Name'   => 'Test',
   'Value'  => '9',
   'Made'   => 'New',
   'Stackable' => true
];
$array['Drinks']['Vodka'] = array_merge([$array['Drinks']['Vodka']], [$add]);

https://eval.in/856419

array_merge() https://www.php.net/manual/en/function.array-merge.php

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.