How can I get child value of this collection?

i have collection like this:

array:13 [▼
  "id" => 1
  "sport_id" => 1
  "section_id" => 1
  "slug" => "northern-ireland-premiership"
  "name_translations" => array:2 [▶]
  "has_logo" => true
  "logo" => "https://tipsscore.com/resb/league/northern-ireland-premiership.png"
  "start_date" => "2021-08-27 18:45:00"
  "end_date" => "2022-03-26 23:59:00"
  "priority" => 0
  "host" => array:2 [▶]
  "tennis_points" => 0
  "facts" => array:6 [▶]
]

i want to pick the required values ​​but i can't reach child values, for exmpl:

"name_translations" => array:2 [▼
    "en" => "Premiership"
    "ru" => "Премьершип"
  ]

this is my code:

foreach($collection as $item) {
    $data[] = [
        'ext_id' => $item['id'],
        'sport_id' => $item['sport_id'],
        'section_id' => $item['section_id'],
        'slug' => $item['slug'],
        'logo' => $item['logo']
    ];
}

dd($data);

how to get name_translation "en" value?


Remember to include what you have tried and what isn't working for you.

You can simply access it like everything else $item['name_translations']['en']

foreach($collection as $item) {
        $data[] =[
            'ext_id' => $item['id'],
            'sport_id' => $item['sport_id'],
            'section_id' => $item['section_id'],
            'slug' => $item['slug'],
            'logo' => $item['logo'],
            'en' => $item['name_translations']['en']
        ];
    }
    dd($data);

Assuming your collection is an instance of Collection, a map operation would give you the array you want.

$data = $collection->map(function ($item) {
    return [
        'ext_id' => $item['id'],
        'sport_id' => $item['sport_id'],
        'section_id' => $item['section_id'],
        'slug' => $item['slug'],
        'logo' => $item['logo'],
        'en' => $item['name_translations']['en']
    ];
});

Using short hand closures (PHP >= 7.4)

$data = $collection->map(fn($item) => [
    'ext_id' => $item['id'],
    'sport_id' => $item['sport_id'],
    'section_id' => $item['section_id'],
    'slug' => $item['slug'],
    'logo' => $item['logo'],
    'en' => $item['name_translations']['en']
]);