I'm busy with cleaning a WordPress parse_blocks() array.
Array: Parse blocks
InnerBlocks (0)
InnerBlocks (1)
What I want is making a function which repeats each innerblock. As you see above the array Parse block has innerblocks and those can get also innerblocks (2 times).
I made a simple function cleanBlock($block)
function cleanBlock($block)
{
if (isset($block['blockName']) && $block['blockName'] != '') {
$splitType = explode('/', $block['blockName']);
$innerBlocks = $block['innerBlocks'];
$block = array(
'type' => $splitType[1],
'attrs' => '',
'innerblocks' => $innerBlocks,
);
return $block;
}
}
There you find "innerblock" my idea was to run the cleanBlock($innerBlocks) again, but if I do that it doesn't work because the $block is made before I can get the innerblock, it's hard to explain I hope you know what I mean.
This is what I want, but this code doesn't work at all:
function cleanBlock($block)
{
if (isset($block['blockName']) && $block['blockName'] != '') {
$splitType = explode('/', $block['blockName']);
$block = array(
'type' => $splitType[1],
'attrs' => '',
'innerblocks' => cleanBlock($block['innerBlocks']),
);
return $block;
}
}
After the function I make the final array:
$newPDFarray = [];
foreach ($parseBlocks as $key => $group) {
$block = cleanBlock($group);
$newPDFarray[] = $block;
}
Don't know if this is the right or short solution but for me it works:
function cleanBlock($block)
{
if (isset($block['blockName']) && $block['blockName'] != '') {
$splitType = explode('/', $block['blockName']);
$innerBlocks = [];
foreach ($block['innerBlocks'] as $block) {
$innerBlocks[] = cleanBlock($block);
}
$block = array(
'type' => $splitType[1],
'attrs' => $block['attrs'],
'innerBlocks' => $innerBlocks,
);
return $block;
}
}
$newPDFarray = [];
foreach ($parseBlocks as $key => $group) {
if ($group['blockName'] != '') {
$questionGroup = cleanBlock($group);
$newPDFarray[] = $questionGroup;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.