別の方法。詳細なキーを事前に保存されたものとして使用する:
<?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',
),
)