Solving an Array Reduce Interview Question - in PHP

preview_player
Показать описание

See the code here:

00:00 Introduction
00:43 Solving with Laravel's Collections
11:17 Solving with native PHP
Рекомендации по теме
Комментарии
Автор

Haven't seen you in a while, great video!

aseemlalfakawma
Автор

Thanks for sharing the native version!

sai_charan
Автор

Love the video, thanks Matt! I tend to like using $carry[$name] ??= [] & $carry[$name][$key] ??= 0 just to clean things up, but obviously that's a personal preference. :)

shawnveltman
Автор

Great video! I just noticed the typo in the description :)

sampodlogar
Автор

First before making code look more elegant I'll use $name = isset( $item['name'] ) ? $item['name'] : null; if ( is_null( $name ) ) continue; or do something else, but assuming 'name' index is always present is ask for a NOTICE

bartech
Автор

It's a couple of years since I've used PHP, but can't you simply do `$carry[$name][$key] += $val` (i.e. without initialising it) relying on it adding $val to nothing or maybe `$carry[$name][$key] = ($carry[$name][$key] ?: 0) + $val` to be a little safer?

jezmck
Автор

Hi @Matt

Cool explanation after edit video, suggesttion by Titan.
it could put more value to video if you add Big (O) value of your function. or in simple word it's space 🚀 and time ⌛ complexity.

zgetro
Автор

The non Laravel version is easier to read as per usual

bremg