Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

j00rd's avatar
Level 2

Errors when accessing offset arrays with Strings & INT

Hi guys,

I'm working along with the Video Game Aggregator series, and feel I've got a good understanding of how most things work as there were various fixes that needed to be done since the series is a bit outdated now. However I keep getting errors when I try to access anything on an offset array. I've been pushing these fixes back as I've not been able to find anything solid online in regards to fixing the issue, apart from I think it would've worked in PHP 7.4 but only as it was more forgiving with errors.

 private function formatGameForView($game)                       /* Removes the formatting logic from View. Check item is in array, then append or pluck specific keys */
    {
            return collect($game)->merge([
                'coverImageUrl' => array_key_exists('cover', $game) ? Str::replaceFirst('thumb', 'cover_big', $game['cover']['url']) : 'No picture',

                'social' => [												
                    'instagram' => collect($game['websites'])->filter(fn ($website) => Str::contains($website['url'], 'instagram'))->first(),
                    'twitter' => collect($game['websites'])->filter(fn ($website) => Str::contains($website['url'], 'twitter'))->first(),
                    'facebook' => collect($game['websites'])->filter(fn ($website) => Str::contains($website['url'], 'facebook'))->first(),
                ],				 /* Cannot access offset array of type string on string */
                
                'involvedCompanies' => $game['involved_companies'][0]['company']['name'],  /* Array access offset on int value error 
            ]); 
    }            

This is the method I've got which formats my data, the social details come through the 'websites' endpoint from the IGDB API and I'm trying to access only Instagram, Facebook and Twitter and save them in to the 'Social' array. From what I've read its a problem when putting array elements in to a string but I'm confused because the 'Str::contains' is what looks for the website name as the 'website' end point comes with no specific name just the URL & ID.

     <a href="{{$game['social']['instagram']['url']}}" class="hover:text-gray-400"> </a> */how I am trying to link the url*/

This is how I'm trying to access the url for the social networks. When I dump the game collection there is a 'Social' array key that lists all the details I am requesting it's just displaying them that gives me the error. Any ideas on why?

I also get a similar problem with the involved companies however I get the error "Trying to access array offset on value of type int" so I'm confused on two separate offset arrays.

Any help or even pointers in the right direction are massively appreciated!! Thank you

0 likes
2 replies
j00rd's avatar
Level 2

@Sinnbeck

Of Course, thanks for replying.

I've attached my code at the bottom so you can see the code as is. I am already dumping the $game once it has been formatted, the results it shows contain the social array with the links, this is the results I get from

        dd($this->formatGameForView($game[0]));
Illuminate\Support\Collection {#515 ▼
  #items: array:20 [▼
    "id" => 103281
    "aggregated_rating" => 87.888888888889
    "cover" => array:2 [▶]
    "genres" => "Shooter"
    "involved_companies" => array:3 [▶]
    "name" => "Halo Infinite"
    "platforms" => "PC, XONE, Series X"
    "rating" => 85.404790336658
    "screenshots" => Illuminate\Support\Collection {#484 ▶}
    "similar_games" => array:10 [▶]
    "slug" => "halo-infinite"
    "summary" => "The Master Chief returns in Halo Infinite – the next chapter of the legendary franchise. When all hope is lost and humanity’s fate hangs in the balance, the Mas ▶"
    "videos" => array:5 [▶]
    "websites" => array:11 [▶]
    "coverImageUrl" => "//images.igdb.com/igdb/image/upload/t_cover_big/co2dto.jpg"
    "memberRating" => "85%"
    "criticRating" => "88%"
    "trailer" => "https://youtube.com/embed/ZtgzKBrU1GY"
    "similarGames" => Illuminate\Support\Collection {#487 ▶}
    "social" => array:3 [▼
      "instagram" => array:6 [▼
        "id" => 80093
        "category" => 8
        "game" => 103281
        "trusted" => true
        "url" => "https://www.instagram.com/halo"
        "checksum" => "8650a24a-533f-730e-cd8c-cdcabc62186d"
      ]
      "twitter" => array:6 [▶]
      "facebook" => array:6 [▶]
    ]
  ]
  #escapeWhenCastingToString: false
}

I'm also wondering about this " #escapeWhenCastingToString: false" this only shows since trying to implement the social sites & can't really grasp my head around the meaning of it. What would it be trying to escape?

 public function show($slug)
    {

        $game = Http::withHeaders([                            /* Use HTTP client with headers of API tokens from .env */
            'Client-ID' => env('IGDB_KEY'),
            'Authorization' => env('IGDB_AUTH'),
        ])  ->withBody(                                                     /* Get the game details where the slug matches the slug being passed through.*/
                'fields name, cover.url, platforms.abbreviation, rating, involved_companies,
                slug,genres.name, aggregated_rating, summary, websites.*, videos.*, screenshots.*, 
                similar_games.cover.url, similar_games.name, similar_games.rating,similar_games.platforms.abbreviation, similar_games.slug;
                where slug="'.$slug.'";                                        
                limit 1;',
                'text/plain'
                )
            ->post('https://api.igdb.com/v4/games')->json();
    
        
        
        abort_if(!$game, 404);                                               /* If the game or url does not exist show 404 */

        dd($this->formatGameForView($game[0]));

        return view('show', [
            'game' => $this->formatGameForView($game[0]),
        ]);

        

    }

    private function formatGameForView($game)                       /* Removes the formatting logic from View. Check item is in array, then append or pluck specific keys */
    {
            return collect($game)->merge([
                'coverImageUrl' => array_key_exists('cover', $game) ? Str::replaceFirst('thumb', 'cover_big', $game['cover']['url']) : 'No picture',
                'genres' => array_key_exists('genres', $game) ? collect($game['genres'])->implode('name', ', ') : 'Undefined Genre',   
                'platforms' => array_key_exists('platforms', $game) ? collect($game['platforms'])->implode('abbreviation', ', ') : 'No Platforms Available',     
                'memberRating' => array_key_exists('rating', $game) ? round($game['rating']).'%' : '0%',
                'criticRating' => array_key_exists('aggregated_rating', $game) ? round($game['aggregated_rating']).'%' : '0%',
                'summary' => array_key_exists('summary', $game) ? ($game['summary']) : 'Sorry No Summary Available!',
                'trailer' => array_key_exists('videos', $game) ? 'https://youtube.com/embed/'.$game['videos'][0]['video_id'] : 404,
                //'involvedCompanies' => $game['involved_companies'][0]['company']['name'],
                'screenshots' => array_key_exists('screenshots', $game) ? collect($game['screenshots'])->map(function ($screenshot){
                    return [
                        'big' => Str::replaceFirst('thumb', 'screenshot_big', $screenshot['url']),
                        'huge' => Str::replaceFirst('thumb', 'screenshot_huge', $screenshot['url']),
                    ];
                })->take(9): 'Screenshots Unavilable',
                'similarGames' => array_key_exists('similar_games', $game) ? collect($game['similar_games'])->map(function ($game){
                    return collect($game)->merge([
                        'coverImageUrl' => array_key_exists('cover', $game) ? Str::replaceFirst('thumb', 'cover_big', $game['cover']['url']) : 'No picture',
                        'rating' => isset($game['rating']) ? round($game['rating']).'%' : null,
                        'platforms' => array_key_exists('platforms', $game) ? collect($game['platforms'])->implode('abbreviation', ', ') : null,
                    ]);                  
                })->take(6) : "No Similar Games", 

                    'social' => [
                        'instagram' => collect($game['websites'])->filter(function ($website){
                        return Str::contains($website['url'], 'instagram');
                        })->first(),
                        'twitter' => collect($game['websites'])->filter(function ($website){
                            return Str::contains($website['url'], 'twitter');
                        })->first(),
                        'facebook' => collect($game['websites'])->filter(function ($website){
                            return Str::contains($website['url'], 'facebook');
                        })->first(),
                    ]
                  

                /* 'social' => [
                    'instagram' => collect($game['websites'])->filter(fn ($website) => Str::contains($website['url'], 'instagram'))->first(),
                    'twitter' => collect($game['websites'])->filter(fn ($website) => Str::contains($website['url'], 'twitter'))->first(),
                    'facebook' => collect($game['websites'])->filter(fn ($website) => Str::contains($website['url'], 'facebook'))->first(),
                ],
                
                'involvedCompanies' => $game['involved_companies'][0]['company']['name'],  /* Trying to array access offset on int value error */ 
            ]); 

           
    }            
          

Please or to participate in this conversation.