Godhaze
Godhaze

Reputation: 155

How can I add another array inside this array in php

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?

Upvotes: 1

Views: 46

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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

Upvotes: 4

user3020875
user3020875

Reputation: 41

$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

Upvotes: 3

Related Questions