0

私はこのような配列を持っています:

$array = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);

splitフィールドをパスとして取得することができidます。次に、それらをツリーに並べ替えたいと思います...私はこれを試しました:

$result = array();

foreach($array as $element) {
    $path = explode('.', $element['id']);

    $subtree = $result;
    while(!empty($path)) {
        $path_element = array_shift($path);

        if(empty($path_element)) {
            $subtree['data'] = $element;
        } else {
            if(!is_array($subtree[$path_element])) {
                $subtree[$path_element] = array();
            }
            $subtree = $subtree[$path_element];
        }
    }
}

しかし、私が得るのは、大量の警告と空の$res-Array だけです。

PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: baz in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0

(24行目は$s = $s[$pe];)

ヒントはありますか?

編集:私の希望する出力は次のようになります

$res = array(
  'foo' => array(
    'data' => ...
    'bar' => array(
      'data' => ...
      'bar' => array(
        'data' => ...
      ),
    ),
    'baz' => array(
      'bar' => array(
        'data' => ...
      ),
    ),
  ),
);

data要素は、配列の元の要素です。

4

3 に答える 3

1

以下のコードは、次の結果を生成します。

Array
(
    [foo] => Array
    (
        [data] => ...
        [baz] => Array
            (
                [bar] => Array
                    (
                        [data] => ...
                    )

        )

        [bar] => Array
        (
            [bar] => Array
            (
                [data] => ...
            )
        )
    )
)

一部の変数の名前を変更しました...

$array = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);


$res = array();

foreach($array as $e) {

    $parts = explode('.', $e['id']);

    $temp = &$res;

    foreach($parts as $key => $el) {
        if (!isset($temp[$el])) $temp[$el] = array();

        if ($key == count($parts)-1) $temp[$el] = array('data' =>  '...');
        $temp = &$temp[$el];
    }
}

print_r($res);
于 2013-07-11T12:49:10.570 に答える
0

を使用して、$res を参照する $s を作成し&ます。これにより、値ではなく参照によってデータが渡されます。

$a = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);

$res = array();

foreach($a as $e) {
    $p = explode('.', $e['id']);

    $s = &$res;
    while(!empty($p)) {
        $pe = array_shift($p);
        if(empty($p)) {
            $s['data'] = $e;
        } else {

            if(!isset($s[$pe])) {
                $s[$pe] = array();
            }
            $s = &$s[$pe];
        }
    }
}

echo '<pre>';
print_r($res);
echo '</pre>';

結果は

Array
(
    [foo] => Array
        (
            [data] => Array
                (
                    [id] => foo.bar
                )

            [baz] => Array
                (
                    [data] => Array
                        (
                            [id] => foo.baz.bar
                        )

                )

            [bar] => Array
                (
                    [data] => Array
                        (
                            [id] => foo.bar.bar
                        )

                )

        )

    [data] => Array
        (
            [id] => foo
        )

)
于 2013-07-11T12:47:40.620 に答える
0

再帰があなたの答えです。

次のようなものが必要になります。

<?php

$a = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);

function createTree($path) {
    if ( count($path) === 1 ) {
        return array($path[0]=>array('data'=>'some random data here'));
    } else {
        $currentRoot = array_shift($path);
        return array($currentRoot => createTree($path));
    }
}

$result = array();

foreach ( $a as $item ) {
    $result = array_merge_recursive(
        $result,
        createTree(
            explode('.',$item['id'])
        )
    );
}

// $result now is what you wanted
于 2013-07-11T12:48:47.177 に答える