1. The existing code implementation is as follows:
PHP
<?php
use Illuminate\Support\Arr;
if (Arr::has($schema, 'sections')) {
foreach ($schema['sections'] as $sectionKey => $section) {
foreach ($section['blocks'] as $blockKey => $block) {
if ($block['type'] == 'internal/size-chart/blocks/size-chart') {
Arr::forget($schema, 'sections.' . $sectionKey . '.blocks.' . $blockKey);
$blockOrderKey = array_search($blockKey, $section['block_order']);
if ($blockOrderKey !== false) {
Arr::forget($schema, 'sections.' . $sectionKey . '.block_order.' . $blockOrderKey);
}
}
}
Arr::set($schema, 'sections.' . $sectionKey . '.block_order', array_values(Arr::get($schema, 'sections.' . $sectionKey . '.block_order')));
}
}
return $schema;2. To streamline the implementation using pass by reference, you can replace Arr::forget($schema, ‘sections.’ . $sectionKey . ‘.blocks.’ . $blockKey); with: Arr::forget($section, ‘blocks.’ . $blockKey);
PHP
<?php
use Illuminate\Support\Arr;
if (Arr::has($schema, 'sections')) {
foreach ($schema['sections'] as $sectionKey => &$section) {
foreach ($section['blocks'] as $blockKey => $block) {
if ($block['type'] == 'internal/size-chart/blocks/size-chart') {
Arr::forget($section, 'blocks.' . $blockKey);
$blockOrderKey = array_search($blockKey, $section['block_order']);
if ($blockOrderKey !== false) {
Arr::forget($section, 'block_order.' . $blockOrderKey);
}
}
}
Arr::set($section, 'block_order', array_values(Arr::get($section, 'block_order')));
}
}
return $schema;