skip to Main Content

@if (isset($anime['genres']))
   @foreach ($anime['genres'] as $genre)
      {{ $genre['name'] . ',' }}
   @endforeach
@else
   @foreach (json_decode($anime->genres) as $genre)
      {{  $genre->name . ',' }}
   @endforeach
@endif

I have a condition where the $anime['genres'] is either an array or an object of type stdClass. I want to retrieve the genre name using array notation if genres is an array, and if it’s an object of type stdClass, use $genre->name instead. However, I always encounter the error ‘Cannot use an object of type stdClass as an array’ when $anime['genres'] is an object, instead of entering the ‘else’ condition.

$anime is return data from this function:

    public function getAnimeDetail($slug)
    {
        $cachedAnimeDetail = Redis::get('anime_detail:' . $slug);
        $anime = DB::table('anime')->where('slug', $slug)->first();

        if ($cachedAnimeDetail) {
            return json_decode($cachedAnimeDetail, true);
        }
        if ($anime != null) {
            if ($anime->status != null || $anime->episode_count != null || $anime->duration != null ) {
                return $anime;
            }
        }       

        $response = Http::get('https://this-url-api/api/v1/anime/' . $slug);
        $result = $response->json();
        
        DB::table('anime')->where('slug', $slug)->update([            
            'rating' => $result['data']['rating'],
            'produser' => $result['data']['produser'],
            'type' => $result['data']['type'],
            'status' => $result['data']['status'],
            'episode_count' => $result['data']['episode_count'],
            'duration' => $result['data']['duration'],
            'release_date' => $result['data']['release_date'],
            'studio' => $result['data']['studio'],
            'genres' => $result['data']['genres'],
            'synopsis' => $result['data']['synopsis'],
            'batch' => json_encode($result['data']['batch']),                  
            'episode_lists' => json_encode($result['data']['episode_lists']),    
        ]);
        
        $result['data']['poster'] = $anime->poster;
        $result['data']['slug'] = $anime->slug;

        Redis::set('anime_detail:' . $slug, json_encode($result['data']), 'EX', 3600 * 24);
    
        return $result['data'];
    }

I have attempted to use isset $anime['genres'] and is_array anime['genres'], but I still receive the error ‘Cannot use an object of type stdClass as an array’ instead of entering the ‘else’ condition.

How can I check whether the correct option is $anime['genres'] or **$anime->genre? **Because it can be both so I need the condition the check it.

2

Answers


  1. you can do it if it is an array or object of type stdClass and then access the genre name accordingly.

    @if (isset($anime['genres']))
        @foreach ($anime['genres'] as $genre)
            {{ $genre['name'] . ',' }}
        @endforeach
    @else
        @php
            $genres = is_array($anime->genres) ? $anime->genres : json_decode($anime->genres);
        @endphp
    
        @foreach ($genres as $genre)
            {{ $genre->name . ',' }}
        @endforeach
    @endif
    

    If it’s an array, it will be used directly. If it’s not an array, it will be converted using json_decode, and then you can iterate through the genres using $genre->name.

    Login or Signup to reply.
  2. You can use the is_array() function or is_object() function to check the type of any variable.

    You may also check if variable is instance of specific class type.

       if (isset($variable) && is_object($variable) && $variable instanceof stdClass) {
          $result = json_decode($variable, true);
       } else if (isset($variable) && is_array($variable)) {
          $result = $variable;
       } else {
          // handle invalid type
       }
    

    In your case, $anime can have either array of 'genres' or object.

    @if (isset($anime['genres']))
       @if (is_object($anime['genres']))
          @foreach ($anime['genres'] as $genre)
             {{ $genre->name . ',' }}
          @endforeach
       @elseif (is_array($anime['genres']))
          @foreach ($anime['genres'] as $genre)
             {{ $genre['name'] . ',' }}
          @endforeach
       @endif
    @elseif(isset($anime->genres))
       @foreach (json_decode($anime->genres) as $genre)
          {{ $genre->name . ',' }}
       @endforeach
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search