POST Uint16Array,使用PHP处理数组

时间:2021-06-09 21:16:52

I wish to convert an array in JS to Uint16Array, POST using AJAX, receive in PHP, convert the POST to a PHP array.

我希望将JS中的数组转换为Uint16Array,使用AJAX进行POST,使用PHP接收,将POST转换为PHP数组。

So far:

至今:

JS

JS

var data = [];

for(var j = 0; j < 4800; j++) {
  data.push(j);  
}

bytesToSendCount = data.length;

var bytesArray = new Uint16Array(bytesToSendCount);
for (var i = 0, l = bytesToSendCount; i < l; i++) {
  bytesArray[i] = data[i];
}

  $.ajax({
    type: "POST",
    url: "index.php",
    data: bytesArray,
    contentType: 'application/octet-stream',
    processData: false,
    success: function(data){        
    console.log("ok");
    },
    failure: function(errMsg) {
        alert(errMsg);
    }
  });

PHP

PHP

$data=file_get_contents('php://input');

How can i make $data to a PHP array? Maybe convert it to a ascii string, than convert it to a array?

如何将$数据转换为PHP数组?也许将它转换为ascii字符串,而不是将其转换为数组?

EDIT: The idea is to have a data logger working at 40SPS and send data to server using mobile internet. The problem in sending JSON or string or something isnit binary, its consuming a lot of data (24kb every two minutes), so using Uint16Array (the logger works at 16bits) reduce the POST to ~9kb/2minutes.

编辑:我们的想法是让数据记录器工作在40SPS,并使用移动互联网将数据发送到服务器。发送JSON或字符串或者其他东西的问题不是二进制,它消耗了大量数据(每两分钟24kb),所以使用Uint16Array(记录器工作在16位)将POST减少到~9kb / 2分钟。

1 个解决方案

#1


1  

Use unpack in PHP.

在PHP中使用unpack。

$array = unpack('n*', $data); // big-endian

or

要么

$array = unpack('v*', $data); // little-endian

It's machine-dependent whether the data is sent in big-endian or little-endian order. You may want to use Dataview so you can control this, then use the appropriate format in PHP.

无论数据是以big-endian还是little-endian顺序发送,都与机器有关。您可能希望使用Dataview以便控制它,然后在PHP中使用适当的格式。

#1


1  

Use unpack in PHP.

在PHP中使用unpack。

$array = unpack('n*', $data); // big-endian

or

要么

$array = unpack('v*', $data); // little-endian

It's machine-dependent whether the data is sent in big-endian or little-endian order. You may want to use Dataview so you can control this, then use the appropriate format in PHP.

无论数据是以big-endian还是little-endian顺序发送,都与机器有关。您可能希望使用Dataview以便控制它,然后在PHP中使用适当的格式。