如何递归地将关联数组项复制到另一个单个数组项?

时间:2014-08-31 08:24:05

标签: php arrays recursion associative-array

我有一个关联数组,其键的编号类似于软件版本号,例如32.1,我想要做的是采用32.1之后的每个版本(32.2,32.3,32.4)等等,并将它们分配给32键下的另一个数组,更多的是我需要脚本递归,以便具有多个版本的较长版本号最终在正确的数组键中。

我的代码......

<?php

    $array = array(
        '1' => 'Level 1',
        '1-1' => 'Level 1',
        '1-2' => 'Level 1',
        '1-2-1' => 'Sub-Level 1',
        '2' => 'Level 2',
        '3' => 'Level 3'
    );

    foreach ($array as $key => $value) {
        static $assoc = array();

        if (preg_match('/-/', $key)) {
            $points = explode('-', $key);
            $assoc_key = basename($key, '-' . end($points));
            $assoc[$assoc_key]['sub'][$key] = array('1' => 'Sub-' . $array[end($points)]);
        } else {
            $assoc[$key] = array('1' => $array[$key]);
        }
    }

    echo '<pre>' . print_r($assoc, true) . '</pre>';

?>

预期输出:

Array
(
  [1] => Array
    (
      [1] => Level 1
      [sub] => Array
        (
          [1-1] => Array
            (
              [1] => Sub-Level 1
            )

          [1-2] => Array
            (
              [1] => Sub-Level 2
              [sub] => Array
                (
                  [1-2-1] => Array
                    (
                      [1] => Sub-Sub-Level 1
                    )

                )

            )

        )

    )

  [2] => Array
    (
      [1] => Level 2
    )

  [3] => Array
    (
      [1] => Level 3
    )

)

实际输出

Array
(
  [1] => Array
    (
      [1] => Level 1
      [sub] => Array
        (
          [1-1] => Array
            (
              [1] => Sub-Level 1
            )

          [1-2] => Array
            (
              [1] => Sub-Level 2
            )

        )

    )

  [1-2] => Array
    (
      [sub] => Array
        (
          [1-2-1] => Array
            (
              [1] => Sub-Level 1
            )

        )

    )

  [2] => Array
    (
      [1] => Level 2
    )

  [3] => Array
    (
      [1] => Level 3
    )

)

有人可以帮忙吗?

我只是知道这很简单,我很想念,但我一直试图让它工作两天。

2 个答案:

答案 0 :(得分:2)

我提出了一些可以使用数组引用解决问题的方法。

可能代码也可以进一步优化,但应足以显示概念。

正如您所说,该函数必须是递归的,但您使用了迭代方法。

<?php
  $array = array(
    '1' => 'Level 1',
    '1-1' => 'Level 1',
    '1-2' => 'Level 1.2',
    '1-2-1' => 'Level 1.2.1',
    '2' => 'Level 2',
    '3' => 'Level 3'
  );

  $assoc = array();

  foreach ($array as $key => $value) {
    if (strpos($key, "-") !== false) {
      $points  = explode('-', $key);
      $current = &$assoc[$points[0]];
      $str     = $points[0];
      for ($i = 1; $i < count($points); $i++) {
        $str    .= "-" . $points[$i];
        if (!isset($current["sub"]))
          $current["sub"] = array();
        $current = &$current["sub"][$str];
      }
      $current[1] = 'Sub-' . $value;
    }
    else {
      $assoc[$key][1] = $array[$key];
    }
  }


  echo '<pre>' . print_r($assoc, true) . '</pre>';
?>

答案 1 :(得分:0)

**已编辑:**您获得此类结果是因为当您构建$assoc_key时,如果您有$ assoc_key =&#39; 1-2&#39;你不应该构建$assoc[$assoc_key]['sub'][$key] = array('1' => 'Sub-' . $array[end($points)]);,因为它会创建一个新的数组,其基数为id&#39; 1-2&#39;你应该创建$assoc[$pre_asscokey]['sub'][$assoc_key][$key] = array('1' => 'Sub-' . $array[end($points)]);  所以试试这个:

   <?php

    $array = array(
        '1' => 'Level 1',
        '1-1' => 'Level 1',
        '1-2' => 'Level 1',
        '1-2-1' => 'Sub-Level 1',
        '2' => 'Level 2',
        '3' => 'Level 3'
    );
    static $assoc = array();
    foreach ($array as $key => $value) {


        if (preg_match('/-/', $key)) {
            $points = explode('-', $key);
            $assoc_key = basename($key, '-' . end($points)); 
            $x=$points[0];
            if($assoc_key == $pre_asscokey  && sizeof($points)==3){
          $assoc[$x]['sub'][$pre_asscokey]['sub'][$key] = array('1' => 'Sub-' . $array[end($points)]);
            }else{
            $assoc[$assoc_key]['sub'][$key] = array('1' => 'Sub-' . $array[end($points)]);
         $pre_asscokey = $key;
            }
        } else {
            $assoc[$key] = array('1' => $array[$key]);
            $pre_asscokey=$key;
        }
    }

    echo '<pre>' . print_r($assoc, true) . '</pre>';

?>

输出:

Array
(
    [1] => Array
        (
            [1] => Level 1
            [sub] => Array
                (
                    [1-1] => Array
                        (
                            [1] => Sub-Level 1
                        )

                    [1-2] => Array
                        (
                            [1] => Sub-Level 2
                            [sub] => Array
                                (
                                    [1-2-1] => Array
                                        (
                                            [1] => Sub-Level 1
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [1] => Level 2
        )

    [3] => Array
        (
            [1] => Level 3
        )

)