简体   繁体   中英

Convert text file in to 2d javascript array using ajax & php

So, I'm building myself a browser based rpg using Javascript. Originally, my level had a single layer and was loaded from a javascript 2d map array. However, I'm changing my code to allow support for multiple layers loaded from a file.

I can retrieve the file data without too many problems, however, I have no idea how to parse the information into useable arrays.

The contents of my text file are as follows;

LAYER
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
LAYER
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0

My Ajax and PHP for retrieving the level;

// JAVASCRIPT
    $.ajax({
            type: 'POST',
            url: 'lib/ajax.php',
            data: {method: 'getLevel'},
            success: function(data){

            },
            error: function(x, h, r){
                console.log(x, h, r);
            }
        })

// PHP FILE 2

public function getLevel(){
   $file = file_get_contents('../levels/level1.txt');
   echo $file;
}

There is an intermediate file handling all of my ajax requests, passing them to a functions class.

I can get my level data fine, I just don't know what to do with it once I have it.

I know I can get someway towards achieving this by adding newline characters at the end of each group, and parsing them that way. However, this will become a nightmare when implementing a tile editor in the future. Do you guys have any suggestions how to go about this? Would you suggest parsing at the php or javascript level, or both?

If you data is following this layout

LAYER
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
LAYER2
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0

You can just do it like this

function parseLayer($text){
    $layers = array();
    $lines = explode("\n", $text);
    $lastLayer;

    $currArray = array();

    foreach($lines as $line){
        if(strpos($line, ",") === false){
            if(!empty($lastLayer)){
                $layers[$lastLayer] = $currArray;
                $currArray = array();
            }
            $lastLayer = trim($line);
        }else{
            $nodes = explode(",", $line);
            $nodeList = array();
            foreach($nodes as $node){
                $nodeList[] = trim($node);
            }
            $currArray[] = $nodeList;
        }
        $layers[$lastLayer] = $currArray;
    }
    return $layers;
}

Then to pass it to Javascript you can use JSON for php json_encode

Since @Mike is on his phone here is the code for you:

{"LAYER":[
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
],
"LAYER2":[
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM