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

Xanger's avatar

Undefined array key "count"

I'm using a package in a Jobs, but it keeps failing:

ErrorException: Undefined array key "count" in /var/www/vhosts/***.it/httpdocs/vendor/askancy/howlongtobeat/src/HowLongToBeat.php:74

Basically the verification error when I search for an item that does not exist, but why does it fail all the jobs instead of just skipping it? The package is: https://github.com/Askancy/howlongtobeat

In my jobs I have tried to insert several controls as well, but it keeps failing, giving me the error array key count...

		Games::chunk($chunkSize, function ($games) {
			foreach ($games as $game) {
				$hl = HowLong2Beat::where('id_game', $game->id)->firstOrFail();

					if ($hl) {
						/* If the game exists let's go by ID */

						$hl2b = new HowLongToBeat();
						$results = $hl2b->get($hl->id_hltb);

						$hl->id_game = $game->id;
						$hl->id_hltb = $results['ID'];
						$hl->mainstory = Str::replace(' Hours', '', $results['Summary']['Main Story']);
						$hl->mainextra = Str::replace(' Hours', '', $results['Summary']['Main + Extra']);
						$hl->complete = Str::replace(' Hours', '', $results['Summary']['Completionist']);
						$hl->save();
					} else {
						/* 
							If it doesn't exist we query HowLongToBeat.
							We check if the searched game exists in their database 
						*/
						$hl2b = new HowLongToBeat();
						$result = $hl2b->search(htmlspecialchars_decode($game->nome));
						if (count($result['Results']) > '1' ) {
							/* Se il risultato esiste */
							$hl = new HowLong2Beat();
							$results = $result['Results'][0];
							$hl->id_game = $game->id;
							$hl->id_hltb = $results['ID'];
							$hl->mainstory = Str::replace(' Hours', '', $results['Summary']['Main Story']);
							$hl->mainextra = Str::replace(' Hours', '', $results['Summary']['Main + Extra']);
							$hl->complete = Str::replace(' Hours', '', $results['Summary']['Completionist']);
							$hl->save();
							logger('Error-hl2b: '.$game->id);
						} else {
							
						}
					}
					
			}
		});

Do you have any advice for solving this problem?

0 likes
11 replies
Snapey's avatar

you are going to have to give us the full error. The code you posted does not try to get an array key of count

Xanger's avatar

@snapey

Here is the complete error...

ErrorException: Undefined array key "count" in /var/www/vhosts/****.it/httpdocs/vendor/askancy/howlongtobeat/src/HowLongToBeat.php:74
Stack trace:
#0 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(254): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError()
#1 /var/www/vhosts/****.it/httpdocs/vendor/askancy/howlongtobeat/src/HowLongToBeat.php(74): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}()
#2 /var/www/vhosts/****.it/httpdocs/app/Jobs/hltbj.php(33): IvanKayzer\HowLongToBeat\HowLongToBeat->search()
#3 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(53): App\Jobs\hltbj->App\Jobs\{closure}()
#4 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(23): Illuminate\Database\Eloquent\Builder->chunk()
#5 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2335): Illuminate\Database\Eloquent\Model->forwardCallTo()
#6 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2347): Illuminate\Database\Eloquent\Model->__call()
#7 /var/www/vhosts/****.it/httpdocs/app/Jobs/hltbj.php(55): Illuminate\Database\Eloquent\Model::__callStatic()
#8 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Jobs\hltbj->handle()
#9 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#10 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#11 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#12 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\Container\BoundMethod::call()
#13 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\Container\Container->call()
#14 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}()
#15 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#16 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then()
#17 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): Illuminate\Bus\Dispatcher->dispatchNow()
#18 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}()
#19 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#20 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(126): Illuminate\Pipeline\Pipeline->then()
#21 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(70): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware()
#22 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(98): Illuminate\Queue\CallQueuedHandler->call()
#23 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(434): Illuminate\Queue\Jobs\Job->fire()
#24 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(384): Illuminate\Queue\Worker->process()
#25 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(175): Illuminate\Queue\Worker->runJob()
#26 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(137): Illuminate\Queue\Worker->daemon()
#27 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(120): Illuminate\Queue\Console\WorkCommand->runWorker()
#28 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#29 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#30 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#31 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#32 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\Container\BoundMethod::call()
#33 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Command.php(194): Illuminate\Container\Container->call()
#34 /var/www/vhosts/****.it/httpdocs/vendor/symfony/console/Command/Command.php(312): Illuminate\Console\Command->execute()
#35 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Command.php(164): Symfony\Component\Console\Command\Command->run()
#36 /var/www/vhosts/****.it/httpdocs/vendor/symfony/console/Application.php(1040): Illuminate\Console\Command->run()
#37 /var/www/vhosts/****.it/httpdocs/vendor/symfony/console/Application.php(314): Symfony\Component\Console\Application->doRunCommand()
#38 /var/www/vhosts/****.it/httpdocs/vendor/symfony/console/Application.php(168): Symfony\Component\Console\Application->doRun()
#39 /var/www/vhosts/****.it/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(200): Symfony\Component\Console\Application->run()
#40 /var/www/vhosts/****.it/httpdocs/artisan(37): Illuminate\Foundation\Console\Kernel->handle()
#41 {main}
automica's avatar

The count error is on your class not on the snippet you have supplied.

Can you post the whole file of your HowLongToBeat.php

automica's avatar

@Xanger

Error corresponds to the following:

                return [
            'Results' => $games,
            'Pagination' => [
                'Total Results' => $searchResult['count'],
                'Current Page' => $page,
                'Last Page' => $searchResult['pageTotal'],
            ]

Where are you defining the count key of $searchResult ?

Xanger's avatar

@automica I tried with:

        $count = isset($searchResult['count']) ? $searchResult['count'] : 0;

and

'Total Results' => $count,

But it continues to fail.

I noticed that Jobs tries 5 times before ending, some times from error: "undefined array key 0" other times "undefined array key "count"

Snapey's avatar

The issue is with the package. You should raise it with the author.

The confusing part for me is that hltb does not seem to have an API !

1 like
Xanger's avatar

@Snapey If it doesn't use the API, use the scraper. there is no ToS that prohibits it.

Snapey's avatar

@Xanger oh sorry, i mistook this for a professional question not a hobbyist

Please or to participate in this conversation.