How to get data from a form and play in a PHP Array

1

I have an Android application, which creates a TXT file in PHP, bringing the XML form data from Android itself. Look at the code:

$f = fopen('POST_DATA.txt', 'a');
    fwrite($f, 'ID: '.$id."\r\n");
    $id = uniqid( time() );
    fwrite($f, 'Nome: '.$_POST['nome']."\r\n");
    fwrite($f, 'Cpf: '.$_POST['cpf']."\r\n");
    fwrite($f, 'Bairro: '.$_POST['bairro']."\r\n");
    fwrite($f, 'E-mail: '.$_POST['email']."\r\n");
    fwrite($f, 'Telefone: '.$_POST['telefone']."\r\n\r\n");

    fclose($f);

I would like to play the data name, cpf, neighborhood, email and phone in an array, which returns the following data, since the query below I can already read it on Android:

$json_str = '{"usuarios": '.'[{"nome":"Felipe", "bairro": São Pedro, "cpf": "11111111", "email" : "[email protected]", "telefone" : "222222222"},'.']}'; 
//faz o parsing da string, criando o array "empregados" 
$jsonObj = json_decode($json_str); $empregados = $jsonObj->empregados; 
    echo $json_str;
    
asked by anonymous 19.06.2015 / 14:17

1 answer

1

Would not it be easier to already bring this json data from your Android app and just read with json_decode?

If this is not possible, do so with a regex:

$linhas = file('POST_DATA.txt');
$ret = array();
foreach ($linhas as $val) {
    preg_match('/^([\w-]+?): ?(.*)/', $val, $matches);
    if (count($matches) === 0) {
        continue; // linha inválida
    }

     $ret[$matches[1]] = $matches[2];
}
var_dump($ret);
    
20.08.2015 / 19:48