【问题标题】:How to convert a string of numbers to an array如何将一串数字转换为数组
【发布时间】:2015-04-23 10:03:21
【问题描述】:

我有一个相当大的 JavaScript 数组,其中包含 -10 到 10 之间的整数。我想将它们保存在数据库中,所以我必须使用 jQuery $.post 函数将它们发送到 PHP;但是,数组太大了,所以我将数组转换为 JavaScript 中的字符串。 我想将它转换回 PHP 中的整数数组。我怎样才能做到这一点?

这是我的代码,到目前为止:

function updateDb(){
for(i = 0; i < inputSquares.length; i++) {
    for(j = 0; j <= 9; j++) {
        perceptronsWeights[10*i+j] = inputSquares[i].weights[j]
    };
};

var pws = String(perceptronsWeights);
alert(typeof pws);

$.post("updateDatabase.php", {size: size, weights:pws}).done (
    function(data) {
        alert (data);
    }
)

而且,在我的 PHP 文件中,我使用:

$weightsString = $_POST['weights'];
$pws = array_map("intval", explode(",", $weightsArray));
echo $pws;

我也试过explode() 没有array_mapstr_split,但他们都给我这个错误:

注意:
中的数组到字符串的转换 C:\xampp\htdocs\ANN_JS\updateDatabase.php 第 19 行
数组

第 19 行是echo $pws;

【问题讨论】:

    标签: javascript php arrays string type-conversion


    【解决方案1】:

    使用print_rvar_dump 回显$pws 的数组详细信息。 如果你想通过echo 试试

    `echo implode(" ",$pws);`
    

    【讨论】:

      【解决方案2】:

      使用print_r($pws) 打印array

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-18
        • 2021-06-14
        • 1970-01-01
        • 2023-03-18
        • 2022-01-24
        • 1970-01-01
        • 2011-02-28
        相关资源
        最近更新 更多