3

I don't know if it's the best or right way to do it, but in PHP, I simply would.

        $array = [];

        $array['CODE-123'][] = [
            'name' => 'NAME 001',
            'lastname' => 'LASTNAME 001',
        ];

        $array['CODE-456'][] = [
            'name' => 'NAME 004',
            'lastname' => 'LASTNAME 004',
        ];

php array

        $array['CODE-123'][] = [
            'name' => 'NAME 002',
            'lastname' => 'LASTNAME 002',
        ];


        $array['CODE-456'][] = [
            'name' => 'NAME 005',
            'lastname' => 'LASTNAME 005',
        ];

php array append by key

But the point is: How to proceed to get the same result but using Laravel Collection? That is, how can I add/append new elements using the 'desired key' from a Laravel Collection?

If I try to use the put(), it overwrites the original key content.

        $collection = collect();

        $collection->put('CODE-123', [
            'name' => 'NAME 001',
            'lastname' => 'LASTNAME 001',
        ]);

        $collection->put('CODE-456', [
            'name' => 'NAME 004',
            'lastname' => 'LASTNAME 004',
        ]);

enter image description here

For example, I want to add some new items, in the key 'CODE-123', if it were a native PHP array, I would use a pair of square brackets, but using collection, I couldn't and it generates an ErrorException (E_NOTICE) Indirect modification of overloaded element.

$collection['CODE-123'][] = 'some new item...';
Magno Alberto
  • 628
  • 14
  • 27

1 Answers1

2

First, I think your initial declarations are missing a set of brackets that would be required to have multiple sets of data stored in a single item.

$collection->put('CODE-123', [
    [
        'name' => 'NAME 001',
        'lastname' => 'LASTNAME 001',
    ]
]);

But what is really sounds like you are trying to do is create a Collection of Collections. So why fight it?

$collection = collect();

$collection->put('CODE-123', collect([
    [
        'name' => 'NAME 001',
        'lastname' => 'LASTNAME 001',
    ]
]));

$collection->put('CODE-456',  collect([
    [
        'name' => 'NAME 004',
        'lastname' => 'LASTNAME 004',
    ]
]));

$value = [
    'name' => 'NAME 002',
    'lastname' => 'LASTNAME 002',
];

$collection['CODE-123']->push($value);

dd($collection);
Collection {#1417 ▼
  #items: array:2 [▼
    "CODE-123" => Collection {#1406 ▼
      #items: array:2 [▼
        0 => array:2 [▼
          "name" => "NAME 001"
          "lastname" => "LASTNAME 001"
        ]
        1 => array:2 [▼
          "name" => "NAME 002"
          "lastname" => "LASTNAME 002"
        ]
      ]
    }
    "CODE-456" => Collection {#1414 ▼
      #items: array:1 [▼
        0 => array:2 [▼
          "name" => "NAME 004"
          "lastname" => "LASTNAME 004"
        ]
      ]
    }
  ]
}
matticustard
  • 4,850
  • 1
  • 13
  • 18
  • 1
    Thank you very much for the code, I tried everything , but not your suggestion, because since the IDE didn't recognize the `push()` referenced after the square brackets, I just gave up to confirm if PHP would interpret correctly. Works like a charm! – Magno Alberto Oct 07 '19 at 18:08