Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从该字符串返回多维数组?_Php_Minecraft - Fatal编程技术网

Php 如何从该字符串返回多维数组?

Php 如何从该字符串返回多维数组?,php,minecraft,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

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

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,所以我不是很好
请帮助我做到这一点

这是不可能的。您无法分辨哪个元素包含在哪个元素中。

有点笨拙,但可以工作

<?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 );

这就足够了,还是您需要额外设置所有参数(x、y、z、俯仰、偏航…)

$a='end:world:world\u the_end x:127.92422698364012 y:49.0 z:16.12629933731299偏航:181.38281俯仰:14.804417沙子:world:world x:-1641.5 y:71.0 z:-26.5偏航:90.90824俯仰:18.818556蜘蛛:world:world x:31.30000011920922 y:4.0 z:1166.8782895110098偏航:5俯仰:5.1715555僵尸:world:x:34.6999998807907 y:5.0 z:1199.704677717212偏航:99.74651节距:8.739505旧:世界:世界x:-53.65626180992732 y:73.0 z:196.10301015333587偏航:116.572773节距:9.224916端3:世界:世界x端:286.3926594887 y:1.0 z:13.586770822063027偏航:269.05328节距:14.14675 S:世界:世界:世界x端:282829.890节距:66.2732001448261偏航:106.30098节距:39.299957阴:世界阴x:-191.956500873528467 y:49.0 z:-52.69999988079064偏航:22.797272节距:6.7500987端杀:世界阴:世界阴x:323.4613136245915 y:1.0 z:5.489686192901858偏航:100.90347节距:3.6467855钢轨:世界阴:28369979807偏航:318.45834俯仰:17.768522火焰:世界:世界下x:-253.02856795676604 y:53.0 z:-100.26892458131793偏航:91.04793俯仰:6.150008地雷2:世界:世界x:-352.58729110805086 y:72.0 z:53.69999988079064偏航:27.53363俯仰:22.949997’;
preg_match_all('/([^:::+:[^::::+:[^::::+:[^::+:+:[^:+:[^:+:[^:{0,})+y:\d{0,}(.{d{0,})+z:\d{0,}偏航:\d{0,}(.\d{0,})+音高:\d{0,}(.{d{0,}+)/sim',$a,$result,$preg模式);
对于($i=0;$i
另一种方法。使用预先存储的详细密钥:

<?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 );

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

$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"
  }
}

你的输入真的没有比这更多的结构吗?此外,您需要指定输出的外观(给出一个示例)。输入不清晰(未结构化),输出未定义。。。你尝试过什么?这是一个来自Minecraft的数据模式输出,可以匹配和分割it@ZathrusWriter是的,我想那可能是一堆游戏数据。在问题中提到这一点会很有帮助!这样标记。@从这里我为什么不考虑重新标记。。。好主意,谢谢!:)这对示例数据有效,但对动态生成的数据可能无效,因为您预先定义了所有键。。。但它应该得到+1:)现在,因为它似乎来自minecraft,我想最好预先定义较低的键,而不是较高的键。制作一个世界阵列,x,y,z,俯仰,偏航等。也许如果你玩过Minecraft,你会看到一个模式^^^^哦,你可以说,向前看一点。请看这可能是最好的答案:)编辑:添加了输出转储不幸的是,我得到的是“未定义变量:rootkey”,似乎没有触发if语句中rootkey的设置,不过感谢您的回复@user1725751如果字符串与示例中的不同,则会发生这种情况。它是不是以别的东西开始而不是结束:?
$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"
  }
}