【问题标题】:php condition to filter results from json dataphp条件过滤json数据的结果
【发布时间】:2019-10-18 18:55:16
【问题描述】:

我正在尝试从 payment_status = 已付款的数据库中获取 grand_total 结果,请查看我的数据库结构和代码,它没有返回已返回所有结果的已付款状态付款结果

这是数据库结构和数据

   ref_id |            payment_status          |    grand_total 
     19   |     [{"admin":"","status":"due"}]  |        100
     19   |     [{"admin":"","status":"paid"}] |        50
     19   |     [{"admin":"","status":"paid"}] |        500

这是我目前的功能

function total_referral_purchase($referral_id)
{
    $return = 0;
    $sales  = $this->db->get('sale')->result_array();
    foreach ($sales as $row) {
         $payment_status = json_decode($row['payment_status'],true);
         $status = $payment_status['status'];

        if ($row['ref_id'] == $referral_id && $status == 'paid' ) {
            $return += $row['grand_total'];
        }
    }
    return $this->cart->format_number($return);
}

我得到的结果 = 0

预期结果 = 550

编辑:

var_dump array(1) { 
    [0]=> array(20) { 
        ["sale_id"]=> string(3) "202" 
        ["sale_code"]=> string(9) "201906202" 
        ["buyer"]=> string(1) "1" 
        ["guest_id"]=> NULL 
        ["ref_id"]=> string(2) "19" 
        ["commission"]=> string(4) "8.83" 
        ["grand_total"]=> string(5) "83.63"
        ["payment_status"]=> string(30) "[{"admin":"","status":"paid"}]" 
        ["payment_details"]=> string(4) "none" 
        ["payment_timestamp"]=> NULL 
        ["sale_datetime"]=> string(10) "1559492618" 
        ["delivary_datetime"]=> string(0) "" 
        ["delivery_status"]=> string(65) "[{"admin":"","status":"pending","comment":"","delivery_time":""}]" 
        ["viewed"]=> string(2) "ok" 
    }
}

【问题讨论】:

  • 对我来说到目前为止没有任何问题,你可以var_dump($sales); 吗?并描述format_number 函数是什么?
  • 能否调试一下“total_referral_purchase($referral_id)”函数的每一行?验证“如果条件”是否满足您的要求。
  • 您还可以在 mysql 上使用 JSON 函数来避免额外的代码,因为它与您所要求的不同,但也可能对您有所帮助:) dev.mysql.com/doc/refman/8.0/en/json-search-functions.html
  • 如果你在你的函数中添加一些var_dump(),你能得到你需要的任何地方吗?例如,一个用于$referral_id,一个用于$sales,在foreach 循环中检查$payment_status$status 和您的$return 在if 之前和之后,也可以检查$row['ref_id']
  • @Frankich => 当我执行 var_dump array(1) { [0]=> array(20) { ["sale_id"]=> string(3) "202" 时,我得到了这个结果["sale_code"]=> string(9) "201906202" ["buyer"]=> string(1) "1" ["guest_id"]=> NULL ["ref_id"]=> string(2) "19" ["commission"]=> string(4) "8.83" ["grand_total"]=> string(5) "83.63"

标签: php mysql json codeigniter


【解决方案1】:

json_decode 函数根据您的 json 格式返回一个数组。 所以你当前代码的结果

json_decode($row['payment_status'],true);

返回此格式的数组

array(1) {
[0]=>
  array(2) {
    ["admin"]=>
    string(0) ""
    ["status"]=>
    string(4) "paid"
  }
}

因为 json 周围的 '[]' 这意味着它将有多个记录并将其解析为数组。 所以 $status 总是为空,应该被替换为

$status = $payment_status[0]['status'];

或者更改数据库中的格式,例如 '{"admin":"","status":"paid"}',以按照您当前的方式工作。

【讨论】:

    【解决方案2】:

    您使用的 JSON 是一个数组,如方括号所示。

    有两种方法可以解决这个问题:

    1. 您将数据库中的 JSON 从 [{"admin":"","status":"due"}] 更改为 {"admin":"","status":"due"}。这使它成为单个 JSON 对象而不是数组。
    2. 而不是$status = $payment_status['status']; 使用$status = $payment_status[0]['status']; 这会从数组中的第一个对象中选择“状态”的值。

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 1970-01-01
      • 2021-11-15
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多