如何从这个字符串返回多维数组?

时间:2012-10-06 20:45:20

标签: php minecraft

您好,我有一个这样的字符串:

end: world: world_the_end x: 127.92422698364012 y: 49.0 z: 16.12629933731299 yaw: 181.38281 pitch: 14.804417 sand: world: world x: -1641.5 y: 71.0 z: -26.5 yaw: 90.90824 pitch: 18.818556 spider: world: world x: 31.300000011920922 y: 4.0 z: 1166.8782895110098 yaw: -0.19852135 pitch: 5.1715555 zombie: world: world x: 34.69999998807907 y: 5.0 z: 1199.704677717212 yaw: 99.74651 pitch: 8.739505 old: world: world x: -53.65626180992732 y: 73.0 z: 196.10301015333587 yaw: 116.57273 pitch: 9.224916 end3: world: world_the_end x: 286.3926594122887 y: 1.0 z: 13.586770822063027 yaw: 269.05328 pitch: 14.146775 chests: world: world x: -9.153748958043282 y: 224.0 z: 66.27320021448261 yaw: -106.30098 pitch: 39.299957 nether: world: world_nether x: -191.95650873528467 y: 49.0 z: -52.699999988079064 yaw: 22.797272 pitch: 6.7500987 endkill: world: world_the_end x: 323.4613136245915 y: 1.0 z: 5.489686192901858 yaw: 100.90347 pitch: 3.6467855 rails: world: world x: -1656.699999988079 y: 10.0 z: -283.69999998807907 yaw: 318.45834 pitch: 17.768522 blaze: world: world_nether x: -253.02856795676604 y: 53.0 z: -100.26892458131793 yaw: 91.04793 pitch: 6.150008 mine2: world: world x: -352.58729110805086 y: 72.0 z: 53.699999988079064 yaw: 27.53363 pitch: 22.949997 

我需要从中返回一个多维数组

等对不起我是php新手所以我不是很好 请帮助我能够做到这一点

5 个答案:

答案 0 :(得分:2)

另一种方式。使用预先存储的详细密钥:

<?php

// make a list of details
$details = array('world','x','y','z','yaw','pitch');
$detailkeys = array_flip( $details );
$str = "end: world: world_the_end x: 127.92422698364012 y: 49.0 z: 16.12629933731299 yaw: 181.38281 pitch: 14.804417 sand: world: world x: -1641.5 y: 71.0 z: -26.5 yaw: 90.90824 pitch: 18.818556 spider: world: world x: 31.300000011920922 y: 4.0 z: 1166.8782895110098 yaw: -0.19852135 pitch: 5.1715555 zombie: world: world x: 34.69999998807907 y: 5.0 z: 1199.704677717212 yaw: 99.74651 pitch: 8.739505 old: world: world x: -53.65626180992732 y: 73.0 z: 196.10301015333587 yaw: 116.57273 pitch: 9.224916 end3: world: world_the_end x: 286.3926594122887 y: 1.0 z: 13.586770822063027 yaw: 269.05328 pitch: 14.146775 chests: world: world x: -9.153748958043282 y: 224.0 z: 66.27320021448261 yaw: -106.30098 pitch: 39.299957 nether: world: world_nether x: -191.95650873528467 y: 49.0 z: -52.699999988079064 yaw: 22.797272 pitch: 6.7500987 endkill: world: world_the_end x: 323.4613136245915 y: 1.0 z: 5.489686192901858 yaw: 100.90347 pitch: 3.6467855 rails: world: world x: -1656.699999988079 y: 10.0 z: -283.69999998807907 yaw: 318.45834 pitch: 17.768522 blaze: world: world_nether x: -253.02856795676604 y: 53.0 z: -100.26892458131793 yaw: 91.04793 pitch: 6.150008 mine2: world: world x: -352.58729110805086 y: 72.0 z: 53.699999988079064 yaw: 27.53363 pitch: 22.949997";

$data = array();
// explode string by space
$fields = explode(' ', $str );

// go through parameters one by one
foreach( $fields as $field )
{
        // does it have colon ?
        if( preg_match('/:$/', $field ))
        {
                // yes. remove colon
                $field = substr( $field, 0, strlen($field)-1);
                // is it detailkey
                if( isset( $detailkeys[$field] ))
                        $key = $field;  // store as key
                else
                        $rootkey = $field;  // otherwise store as rootkey
                continue;
        } else

        // does not have colon. Let's store a value.
        $val = $field;
        $data[$rootkey][$key] = $val;
}

// all done, echo output

var_export( $data );

编辑:

将提供此输出:

array (
  'end' => 
  array (
    'world' => 'world_the_end',
    'x' => '127.92422698364012',
    'y' => '49.0',
    'z' => '16.12629933731299',
    'yaw' => '181.38281',
    'pitch' => '14.804417',
  ),
  'sand' => 
  array (
    'world' => 'world',
    'x' => '-1641.5',
    'y' => '71.0',
    'z' => '-26.5',
    'yaw' => '90.90824',
    'pitch' => '18.818556',
  ),
  'spider' => 
  array (
    'world' => 'world',
    'x' => '31.300000011920922',
    'y' => '4.0',
    'z' => '1166.8782895110098',
    'yaw' => '-0.19852135',
    'pitch' => '5.1715555',
  ),
  'zombie' => 
  array (
    'world' => 'world',
    'x' => '34.69999998807907',
    'y' => '5.0',
    'z' => '1199.704677717212',
    'yaw' => '99.74651',
    'pitch' => '8.739505',
  ),
  'old' => 
  array (
    'world' => 'world',
    'x' => '-53.65626180992732',
    'y' => '73.0',
    'z' => '196.10301015333587',
    'yaw' => '116.57273',
    'pitch' => '9.224916',
  ),
  'end3' => 
  array (
    'world' => 'world_the_end',
    'x' => '286.3926594122887',
    'y' => '1.0',
    'z' => '13.586770822063027',
    'yaw' => '269.05328',
    'pitch' => '14.146775',
  ),
  'chests' => 
  array (
    'world' => 'world',
    'x' => '-9.153748958043282',
    'y' => '224.0',
    'z' => '66.27320021448261',
    'yaw' => '-106.30098',
    'pitch' => '39.299957',
  ),
  'nether' => 
  array (
    'world' => 'world_nether',
    'x' => '-191.95650873528467',
    'y' => '49.0',
    'z' => '-52.699999988079064',
    'yaw' => '22.797272',
    'pitch' => '6.7500987',
  ),
  'endkill' => 
  array (
    'world' => 'world_the_end',
    'x' => '323.4613136245915',
    'y' => '1.0',
    'z' => '5.489686192901858',
    'yaw' => '100.90347',
    'pitch' => '3.6467855',
  ),
  'rails' => 
  array (
    'world' => 'world',
    'x' => '-1656.699999988079',
    'y' => '10.0',
    'z' => '-283.69999998807907',
    'yaw' => '318.45834',
    'pitch' => '17.768522',
  ),
  'blaze' => 
  array (
    'world' => 'world_nether',
    'x' => '-253.02856795676604',
    'y' => '53.0',
    'z' => '-100.26892458131793',
    'yaw' => '91.04793',
    'pitch' => '6.150008',
  ),
  'mine2' => 
  array (
    'world' => 'world',
    'x' => '-352.58729110805086',
    'y' => '72.0',
    'z' => '53.699999988079064',
    'yaw' => '27.53363',
    'pitch' => '22.949997',
  ),
)

答案 1 :(得分:1)

有点笨拙但有效。

<?php

// make a list of upper keys
$upper = array('end', 'sand','spider','zombie','old','end3','chests','nether','endkill','rails','blaze','mine2');
$upperkeys = array_flip( $upper );
$str = "end: world: world_the_end x: 127.92422698364012 y: 49.0 z: 16.12629933731299 yaw: 181.38281 pitch: 14.804417 sand: world: world x: -1641.5 y: 71.0 z: -26.5 yaw: 90.90824 pitch: 18.818556 spider: world: world x: 31.300000011920922 y: 4.0 z: 1166.8782895110098 yaw: -0.19852135 pitch: 5.1715555 zombie: world: world x: 34.69999998807907 y: 5.0 z: 1199.704677717212 yaw: 99.74651 pitch: 8.739505 old: world: world x: -53.65626180992732 y: 73.0 z: 196.10301015333587 yaw: 116.57273 pitch: 9.224916 end3: world: world_the_end x: 286.3926594122887 y: 1.0 z: 13.586770822063027 yaw: 269.05328 pitch: 14.146775 chests: world: world x: -9.153748958043282 y: 224.0 z: 66.27320021448261 yaw: -106.30098 pitch: 39.299957 nether: world: world_nether x: -191.95650873528467 y: 49.0 z: -52.699999988079064 yaw: 22.797272 pitch: 6.7500987 endkill: world: world_the_end x: 323.4613136245915 y: 1.0 z: 5.489686192901858 yaw: 100.90347 pitch: 3.6467855 rails: world: world x: -1656.699999988079 y: 10.0 z: -283.69999998807907 yaw: 318.45834 pitch: 17.768522 blaze: world: world_nether x: -253.02856795676604 y: 53.0 z: -100.26892458131793 yaw: 91.04793 pitch: 6.150008 mine2: world: world x: -352.58729110805086 y: 72.0 z: 53.699999988079064 yaw: 27.53363 pitch: 22.949997";

$data = array();
// explode string by space
$fields = explode(' ', $str );

// go through parameters one by one
foreach( $fields as $field )
{
        // does it have colon ?
        if( preg_match('/:$/', $field ))
        {
                // yes. remove colon
                $field = substr( $field, 0, strlen($field)-1);
                // is it upperkey
                if( isset( $upperkeys[$field] ))
                        $rootkey = $field;  // store as rootkey
                else
                        $key = $field;  // otherwise store key
                continue;
        } else

        // dos not have colon. Let's store a vlue.
        $val = $field;
        $data[$rootkey][$key] = $val;
}

// all done, echo output

var_export( $data );

答案 2 :(得分:1)

您可以编写一个小循环来表示存储在字符串中的数据结构,然后从中创建数组:

$string = "end: world: world_the_end x: 127.92422698364012 y: 49.0 z: ...";
$array = null;
$stack = array_reverse(explode(' ', $string));
$stackSize = count($stack);
$section = 0;
while ($stack) {
    $name = substr(array_pop($stack), 0, -1); $stackSize--;
    while ($stack && substr($stack[$stackSize-2], -1) !== ':') {
        $key = substr(array_pop($stack), 0, -1);
        $array[$name][$key] = array_pop($stack);
        $stackSize -= 2;
    }
}
var_dump($array);

将输出:

array(12) {
  'end' =>
  array(6) {
    'world' =>
    string(13) "world_the_end"
    'x' =>
    string(18) "127.92422698364012"
    'y' =>
    string(4) "49.0"
    'z' =>
    string(17) "16.12629933731299"
    'yaw' =>
    string(9) "181.38281"
    'pitch' =>
    string(9) "14.804417"
  }
  'sand' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(7) "-1641.5"
    'y' =>
    string(4) "71.0"
    'z' =>
    string(5) "-26.5"
    'yaw' =>
    string(8) "90.90824"
    'pitch' =>
    string(9) "18.818556"
  }
  'spider' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(18) "31.300000011920922"
    'y' =>
    string(3) "4.0"
    'z' =>
    string(18) "1166.8782895110098"
    'yaw' =>
    string(11) "-0.19852135"
    'pitch' =>
    string(9) "5.1715555"
  }
  'zombie' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(17) "34.69999998807907"
    'y' =>
    string(3) "5.0"
    'z' =>
    string(17) "1199.704677717212"
    'yaw' =>
    string(8) "99.74651"
    'pitch' =>
    string(8) "8.739505"
  }
  'old' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(18) "-53.65626180992732"
    'y' =>
    string(4) "73.0"
    'z' =>
    string(18) "196.10301015333587"
    'yaw' =>
    string(9) "116.57273"
    'pitch' =>
    string(8) "9.224916"
  }
  'end3' =>
  array(6) {
    'world' =>
    string(13) "world_the_end"
    'x' =>
    string(17) "286.3926594122887"
    'y' =>
    string(3) "1.0"
    'z' =>
    string(18) "13.586770822063027"
    'yaw' =>
    string(9) "269.05328"
    'pitch' =>
    string(9) "14.146775"
  }
  'chests' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(18) "-9.153748958043282"
    'y' =>
    string(5) "224.0"
    'z' =>
    string(17) "66.27320021448261"
    'yaw' =>
    string(10) "-106.30098"
    'pitch' =>
    string(9) "39.299957"
  }
  'nether' =>
  array(6) {
    'world' =>
    string(12) "world_nether"
    'x' =>
    string(19) "-191.95650873528467"
    'y' =>
    string(4) "49.0"
    'z' =>
    string(19) "-52.699999988079064"
    'yaw' =>
    string(9) "22.797272"
    'pitch' =>
    string(9) "6.7500987"
  }
  'endkill' =>
  array(6) {
    'world' =>
    string(13) "world_the_end"
    'x' =>
    string(17) "323.4613136245915"
    'y' =>
    string(3) "1.0"
    'z' =>
    string(17) "5.489686192901858"
    'yaw' =>
    string(9) "100.90347"
    'pitch' =>
    string(9) "3.6467855"
  }
  'rails' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(18) "-1656.699999988079"
    'y' =>
    string(4) "10.0"
    'z' =>
    string(19) "-283.69999998807907"
    'yaw' =>
    string(9) "318.45834"
    'pitch' =>
    string(9) "17.768522"
  }
  'blaze' =>
  array(6) {
    'world' =>
    string(12) "world_nether"
    'x' =>
    string(19) "-253.02856795676604"
    'y' =>
    string(4) "53.0"
    'z' =>
    string(19) "-100.26892458131793"
    'yaw' =>
    string(8) "91.04793"
    'pitch' =>
    string(8) "6.150008"
  }
  'mine2' =>
  array(6) {
    'world' =>
    string(5) "world"
    'x' =>
    string(19) "-352.58729110805086"
    'y' =>
    string(4) "72.0"
    'z' =>
    string(18) "53.699999988079064"
    'yaw' =>
    string(8) "27.53363"
    'pitch' =>
    string(9) "22.949997"
  }
}

答案 3 :(得分:0)

这是否足够,或者您是否需要额外包含所有参数(x,y,z,音高,偏航......)?

    $a = 'end: world: world_the_end x: 127.92422698364012 y: 49.0 z: 16.12629933731299 yaw: 181.38281 pitch: 14.804417 sand: world: world x: -1641.5 y: 71.0 z: -26.5 yaw: 90.90824 pitch: 18.818556 spider: world: world x: 31.300000011920922 y: 4.0 z: 1166.8782895110098 yaw: -0.19852135 pitch: 5.1715555 zombie: world: world x: 34.69999998807907 y: 5.0 z: 1199.704677717212 yaw: 99.74651 pitch: 8.739505 old: world: world x: -53.65626180992732 y: 73.0 z: 196.10301015333587 yaw: 116.57273 pitch: 9.224916 end3: world: world_the_end x: 286.3926594122887 y: 1.0 z: 13.586770822063027 yaw: 269.05328 pitch: 14.146775 chests: world: world x: -9.153748958043282 y: 224.0 z: 66.27320021448261 yaw: -106.30098 pitch: 39.299957 nether: world: world_nether x: -191.95650873528467 y: 49.0 z: -52.699999988079064 yaw: 22.797272 pitch: 6.7500987 endkill: world: world_the_end x: 323.4613136245915 y: 1.0 z: 5.489686192901858 yaw: 100.90347 pitch: 3.6467855 rails: world: world x: -1656.699999988079 y: 10.0 z: -283.69999998807907 yaw: 318.45834 pitch: 17.768522 blaze: world: world_nether x: -253.02856795676604 y: 53.0 z: -100.26892458131793 yaw: 91.04793 pitch: 6.150008 mine2: world: world x: -352.58729110805086 y: 72.0 z: 53.699999988079064 yaw: 27.53363 pitch: 22.949997';
    preg_match_all('/([^:]+: [^:]+: [^:]+: \d{0,}(.\d{0,})+? y: \d{0,}(.\d{0,})+? z: \d{0,}(.\d{0,})+? yaw: \d{0,}(.\d{0,})+? pitch: \d{0,}(.\d{0,})+? )/sim', $a, $result, PREG_PATTERN_ORDER);
    for ($i = 0; $i < count($result[0]); $i++) {
        # Matched text = $result[0][$i];
        $arr[] = $result[0][$i];
    }

    var_dump($arr);

答案 4 :(得分:-1)

这种输入是不可能的。你不知道哪个元素包含在哪个元素中。