我在解决这个问题时遇到了真正的问题,所以我希望我能充分解释它!
我有一个元素数组,我需要将它们插入到不同特定位置的数组中。
所以我有这个(也可能有'prependTo'): -
$merged_movers = Array(
[class:ics-header] => insertBefore(class:ics-page-holder)
[class:ics-secondary] => insertAfter(class:ics-primary)
[class:ics-footer] => appendTo(class:ics-page)
[class:ics-full-side-2] => insertBefore(class:ics-page)
)
我需要插入这个: -
$default_structure = Array
(
[class:ics-page-holder] => Array
(
[class:ics-full-side-1] => class:ics-full-side-1
[class:ics-page] => Array
(
[class:ics-topblock] => class:ics-topblock
[class:ics-main] => Array
(
[class:ics-main-top] => class:ics-main-top
[class:ics-primary] => Array
(
[class:ics-primary-top] => class:ics-primary-top
[class:ics-article] => class:ics-article
[class:ics-article2] => class:ics-article2
[class:ics-primary-bottom] => class:ics-primary-bottom
)
[class:ics-tertiary] => class:ics-tertiary
[class:ics-main-bottom] => class:ics-main-bottom
)
[class:ics-bottomblock] => class:ics-bottomblock
)
)
)
我需要结束这个: -
[class:ics-header] => class:ics-header
[class:ics-page-holder] => Array
(
[class:ics-full-side-1] => class:ics-full-side-1
[class:ics-full-side-2] => class:ics-full-side-2
[class:ics-page] => Array
(
[class:ics-topblock] => class:ics-topblock
[class:ics-main] => Array
(
[class:ics-main-top] => class:ics-main-top
[class:ics-primary] => Array
(
[class:ics-primary-top] => class:ics-primary-top
[class:ics-article] => class:ics-article
[class:ics-article2] => class:ics-article2
[class:ics-primary-bottom] => class:ics-primary-bottom
)
[class:ics-secondary] => class:ics-secondary
[class:ics-tertiary] => class:ics-tertiary
[class:ics-main-bottom] => class:ics-main-bottom
)
[class:ics-bottomblock] => class:ics-bottomblock
)
[class:ics-footer] => class:ics-footer
)
我已经尝试了所有可以找到的array_merge、array_merge 递归等,但注意到给了我这个顺序,所以我想做的是遍历第二个数组,如果它的键与第一个数组的值中括号中的一个位匹配,则插入根据键 insertBefore/prependTo 等值从第一个数组中获取元素。
但我只是无法弄清楚如何在某些位置而不是在每个(递归)块的末尾将数组元素添加到第二个数组中......
真的希望有人能给我一些指点...
更新这是我正在尝试的... insertBefore 和 appendTo 似乎工作正常,但 insertAfter 正在追加,我无法弄清楚如何停止它(不确定 prepedTo
$new_default_structure = $default_structure;
foreach($merged_movers as $el => $pos_settings)
{
$pos_setting = explode('(',rtrim($pos_settings, ')'));
$position = $pos_setting[0];
$position_relation = $pos_setting[1];
parse_icl_structure_reset_els(&$new_default_structure, $el, $el_content[$el], $position, $position_relation);
}
它称之为: -
function parse_icl_structure_reset_els(&$default_structure, $el, $el_content, $position, $position_relation, &$insertbefore='', &$insertafter='') {
if($insertbefore != '')
{
foreach($insertbefore as $key2 => $value2)
$default_structure[$key2] = $value2;
$insertbefore = '';
}
foreach($default_structure as $key => &$value)
{
if($key == $position_relation)
{
if($position == 'insertBefore')//this might be working
{
$holdingkey = $value;
unset($default_structure[$key]);
$default_structure[$el.'insertBefore'] = $el_content;
$default_structure[$key] = $value;
if(is_array($value))
parse_icl_structure_reset_els(&$value, $el, $el_content, $position, $position_relation, &$insertbefore, &$insertafter);
}
if($position == 'insertAfter')//this seems to do what I want appendTo to do
{
if(is_array($value))
parse_icl_structure_reset_els(&$value, $el, $el_content, $position, $position_relation, &$insertbefore, &$insertafter);
else
$value[$key] = $value;
$value[$el.'insertAfter'] = $el_content;
}
if($position == 'appendTo')
{
if(is_array($value))
parse_icl_structure_reset_els(&$value, $el, $el_content, $position, $position_relation, &$insertbefore, &$insertafter);
else
$value[$key] = $value;
$value[$el.'appendTo'] = $el_content;
}
}
elseif(is_array($value))
{
parse_icl_structure_reset_els(&$value, $el, $el_content, $position, $position_relation, &$insertbefore, &$insertafter);
}
}
}
这给了我这个
[class:ics-headerinsertBefore] => class:ics-header
[class:ics-page-holder] => Array
(
[class:ics-full-side-1] => class:ics-full-side-1
[class:ics-full-side-2insertBefore] => class:ics-full-side-2
[class:ics-page] => Array
(
[class:ics-topblock] => class:ics-topblock
[class:ics-main] => Array
(
[class:ics-main-top] => class:ics-main-top
[class:ics-primary] => Array
(
[class:ics-primary-top] => class:ics-primary-top
[class:ics-article] => class:ics-article
[class:ics-article2] => class:ics-article2
[class:ics-primary-bottom] => class:ics-primary-bottom
[class:ics-secondaryinsertAfter] => class:ics-secondary
)
[class:ics-tertiary] => class:ics-tertiary
[class:ics-main-bottom] => class:ics-main-bottom
)
[class:ics-bottomblock] => class:ics-bottomblock
[class:ics-footerappendTo] => class:ics-footer
)
)
有任何想法吗?