In Laravel 6, when manipulating a multidimensional array, use reference pass to thin code implementation
1. The existing code is implemented as follows
<?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. Use reference pass to implement in thin code, you can put arr::forget($schema,sections.. $SectionKey ..blocks.. $blockkey); replace with: arr::forget($section,blocks.. $blockkey);
<?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;