$count, 'color' => $contained_color ]; $containers[$i]['contents'] = $all_contained; } ++$i; } $target_color = 'shiny gold'; $possible_containers = []; $highest_count = 0; foreach ( $containers as $container ) { foreach ( $container['contents'] as $content ) { if ( $content['color'] == $target_color ) { $possible_containers[] = $container; break; } } } for ( ;; ) { $top_level_containers = $possible_containers; foreach ( $top_level_containers as $top_level_container ) { $target_color = $top_level_container['name']; foreach ( $containers as $container ) { if ( in_array($container, $possible_containers) ) { continue; } foreach ( $container['contents'] as $content ) { if ( $content['color'] == $target_color ) { $possible_containers[] = $container; break; } } } } if ( count($possible_containers) > $highest_count ) { $highest_count = count($possible_containers); } else { break; } print count($possible_containers) . "\n"; }