PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] How can I get child value of this collection?

 February 21, 2022     arrays, collections, laravel, php     No comments   

Issue

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?


Solution

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);


Answered By - Nicklas Kevin Frank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing