【问题标题】:Parsing array data解析数组数据
【发布时间】:2023-04-11 12:41:01
【问题描述】:

我正在尝试解析从 JQuery 和 JSON 生成的数组数据。

Array
(
[d] => Array
    (
        [0] => Array
            (
                [order] => 1
            )

        [1] => Array
            (
                [order] => 2
            )

        [2] => Array
            (
                [order] => 3
            )

        [3] => Array
            (
                [order] => 4
            )

        [4] => Array
            (
                [order] => 5
            )

    )

)

我正在尝试将上述日期保存到 mysql 表中,但似乎无法正确解析要插入数据库的数据。

谁能帮忙?

【问题讨论】:

    标签: php jquery arrays json


    【解决方案1】:

    你所要做的就是这样,

    foreach ( $data['d'] as $key => $value ) { 
       $id = $value['order'];
       $order = 'order';
       $this->img_model->update_image_order($id , $order); 
    }
    

    这假定 JSON 中的数字是订单 ID。

    【讨论】:

    • 感谢 ZZCoder,不幸的是我遇到了一些错误。我收到“消息:使用未定义的常量 d - 假定为 'd”和“消息:未定义的变量:顺序”错误。我该如何解决这个问题?
    • 我不知道消息的语义,所以代码只是一个如何处理数组数组的示例。刚刚编辑以消除错误。
    【解决方案2】:

    您应该使用 json_decode()(正如其他人已经说明的那样)将您的 JSON 解码为 php 数组。 但是,如果我做对了,您的问题是如何使用 PHP 数组,所以我建议您查看一些不错的基础教程。 w3c 在这里有不错的基本数组教程:http://www.w3schools.com/php/php_arrays.asp

    【讨论】:

      【解决方案3】:

      这看起来像函数print_r() 的输出——并且这不是要被解析的

      您应该直接使用您的 PHP 代码从 Ajax 请求接收到的数据,而不是尝试解析它——即使用 JSON 数据,使用json_decode() 解码。

      【讨论】:

      • 哇,这么快的回复...这是通过 AJAX POST 发送的内容:{"d":[{"order":1},{"order":2},{" order":3},{"order":4},{"order":5}]} 并使用 json_decode,下面的代码是我目前所拥有的: $data = file_get_contents('php://input') ; $data = json_decode(utf8_decode($data), true); foreach ( $data[d] as $key => $value ) { // $this->img_model->update_image_order($id , $order);我的主要问题是让我的头绕着数组...希望有人能指出我正确的方向。
      【解决方案4】:

      我想语言是 PHP? 您可能想在这里查看 json_decode()-function:http://php.net/manual/en/function.json-decode.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多