0

我在解决这个问题时遇到了真正的问题,所以我希望我能充分解释它!

我有一个元素数组,我需要将它们插入到不同特定位置的数组中。

所以我有这个(也可能有'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
            )

    )

有任何想法吗?

4

1 回答 1

0

好吧,我想我破解了!

首先要做的是更改 $merged_movers 数组的结构,以便键是已经在默认结构数组中的元素......

foreach($merged_movers as $move_el => $pos_settings)
    {
    $pos_setting = explode('(',rtrim($pos_settings, ')'));
    $position = $pos_setting[0];
    $target = $pos_setting[1];
    $new_merged_movers[$target][$move_el] = $position;  
    }
$merged_movers = $new_merged_movers;

所以现在这个数组看起来像这样

[class:ics-page-holder] => Array
    (
        [class:ics-header] => insertBefore
    )

[class:ics-primary] => Array
    (
        [class:ics-secondary] => insertAfter
    )

[class:ics-page] => Array
    (
        [class:ics-footer] => appendTo
        [class:ics-full-side-2] => insertBefore
    )

然后我调用 $structureOut = parse_icl_structure_reset_els($default_structure, $merged_movers, $prependTo='', $appendTo='');

这是这个功能

function parse_icl_structure_reset_els($default_structure, $merged_movers, $prependTo='', $appendTo='') {
if(is_array($prependTo))
    {
    foreach($prependTo as $el => $val)
        {
        $newStructure[$el] = $el;   
        }
    $prependTo = '';    
    }
foreach($default_structure as $key => $value)
    {
    if(array_key_exists($key,$merged_movers))
        {
        foreach($merged_movers[$key] as $el => $pos)
            {
            if($pos == 'insertBefore')
                {
                $newStructure[$el] = $el;
                }
            elseif($pos == 'prependTo' || $pos == 'appendTo')
                {
                if($pos == 'prependTo')
                    {
                    $prependTo[$el] = $key;
                    }
                if($pos == 'appendTo')
                    {
                    $appendTo[$el] = $key;
                    }
                }
            }
        }
    if(is_array($value))
        {
        $this_content_array = parse_icl_structure_reset_els($value, $merged_movers, $prependTo, $appendTo);
        if(is_array($prependTo))
            {
            foreach($prependTo as $el => $val)
                {
                if($val == $key)
                    {
                    $out_content_array[$el] = $el;
                    $out_modified = true;
                    unset($prependTo[$el]);
                    }   
                }
            if($out_modified == true)
                {
                foreach($this_content_array as $pkey => $pvalue)
                    {
                    $out_content_array[$pkey] = $pvalue;
                    }
                $this_content_array = $out_content_array;
                $out_modified = false;
                }
            }
        if(is_array($appendTo))
            {
            foreach($appendTo as $el => $val)
                {
                if($val == $key)
                    {
                    $this_content_array[$el] = $el; 
                    unset($appendTo[$el]);
                    }
                }   
            }
        $newStructure[$key] = $this_content_array;
        }
    else
        {
        $newStructure[$key] = $value;   
        }
    if(array_key_exists($key,$merged_movers))
        {
        foreach($merged_movers[$key] as $el => $pos)
            {
            if($pos == 'insertAfter')
                {
                $newStructure[$el] = $el;   
                }
            }
        }
    }
return $newStructure;
}

令人惊讶的是,睡个好觉有多大帮助;)

唯一剩下的问题是插入的元素内容本身可能是一个数组......嗯

于 2013-07-06T14:20:36.200 回答