将单词拆分为数组中的每个单词

时间:2015-01-17 20:36:06

标签: php arrays

我尝试在每个字母上分割一个单词,并将每个字母放在上一个数组中。

代码:

$array = [];
$word = "test";
for($i = 0; $i < strlen($word); $i++){
    //  $array[$word[$i]] = ...
    }

我无法理解如何保持&#34;参考&#34;到上一个数组..

我试图得到的(字:&#34;测试&#34;):

    Array
    (
        [t] => Array
            (
                [e] => Array
                    (
                       [s] => Array
                           (
                             [t]
                             [word] => "test"
                            )
                    )
            )
    )

2 个答案:

答案 0 :(得分:0)

$word = 'test';

$root = [];
$array = &$root;

for($i = 0; $i < strlen($word); $i++){
    $ch = $word[$i];
    $new = [];
    $array[$ch] = &$new;
    $array = &$new;
}

$array['word'] = $word;

print_r($root);

我不知道你为什么要这样做,你想尝试做一个特里?

答案 1 :(得分:0)

https://eval.in/243876

$word = "test";
$wordarray = str_split($word);
$result = array();
$tmp = array();


foreach($wordarray as $i => $letter) {
    if($i==0) {
        $tmp[$wordarray[strlen($word)-($i+1)]]["word"] = $word;
    } else {
    $tmp = array();
        $tmp[$wordarray[strlen($word)-($i+1)]] = $result;
    }
    $result = $tmp;
}

var_dump($result);

或者更优雅的方式:

<?php

$word = "test";
$wordarray = array_reverse(str_split($word));
$result = array();
$tmp = array();


foreach($wordarray as $i => $letter) {
    if($i == 0) {
        $tmp[$letter]["word"] = $word;
    } else {
        $tmp = array();
        $tmp[$letter] = $result;
    }
    $result = $tmp;
}

var_dump($result);

https://eval.in/243937

输出

array(1) {
  ["t"]=>
  array(1) {
    ["e"]=>
    array(1) {
      ["s"]=>
      array(1) {
        ["t"]=>
        array(1) {
          ["word"]=>
          string(4) "test"
        }
      }
    }
  }
}

'honorificabilitudinitatibus'一词的输出:

array(1) {
  ["h"]=>
  array(1) {
    ["o"]=>
    array(1) {
      ["n"]=>
      array(1) {
        ["o"]=>
        array(1) {
          ["r"]=>
          array(1) {
            ["i"]=>
            array(1) {
              ["f"]=>
              array(1) {
                ["i"]=>
                array(1) {
                  ["c"]=>
                  array(1) {
                    ["a"]=>
                    array(1) {
                      ["b"]=>
                      array(1) {
                        ["i"]=>
                        array(1) {
                          ["l"]=>
                          array(1) {
                            ["i"]=>
                            array(1) {
                              ["t"]=>
                              array(1) {
                                ["u"]=>
                                array(1) {
                                  ["d"]=>
                                  array(1) {
                                    ["i"]=>
                                    array(1) {
                                      ["n"]=>
                                      array(1) {
                                        ["i"]=>
                                        array(1) {
                                          ["t"]=>
                                          array(1) {
                                            ["a"]=>
                                            array(1) {
                                              ["t"]=>
                                              array(1) {
                                                ["i"]=>
                                                array(1) {
                                                  ["b"]=>
                                                  array(1) {
                                                    ["u"]=>
                                                    array(1) {
                                                      ["s"]=>
                                                      array(1) {
                                                        ["word"]=>
                                                        string(27) "honorificabilitudinitatibus"
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}