如何检索包含括号{}之间数据的MySQL数据库字段作为数组

时间:2016-03-31 07:35:20

标签: php mysql arrays magento datafield

我需要在magento sales_flat_quote_payment中检索此表中的数据。表格中有一个名为additional_data的字段。

additional_data字段中括号{ }

中的数据

additional data

以下是数据格式化的方式

a:6:{
    s:14:"component_mode";
    s:4:"ebay";
    s:14:"payment_method";
    s:6:"PayPal";
    s:16:"channel_order_id";
    s:9:"123654789";
    s:17:"channel_final_fee";
    d:5.3600000000000003;
    s:12:"transactions";
    a:1:{
        i:0;
        a:4:{
            s:14:"transaction_id";
            s:15:"987456321456987";
            s:3:"sum";
            d:66.989999999999995;
            s:3:"fee";
            d:1.5700000000000001;
            s:16:"transaction_date";
            s:19:"2016-03-31 02:54:18";
            }
        }
    s:6:"tax_id";
    s:0:"";
}

我如何像数组一样访问这些数据?

我特别需要从此数据中提取s:3:"fee";的{​​{1}}值。

我试了d:1.5700000000000001;但没有成功。

这是我使用的代码。

$additional_data->transactions->fee

UPdate:我反序列化了数组。现在它看起来像这样。

$con = mysql_connect("localhost","user","password");

if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$po = $_GET['po'];


if( ! is_numeric($po) )
  die('invalid PO#');

$query1 = "SELECT * FROM `sales_flat_quote` WHERE `reserved_order_id` LIKE '" . $po . "' LIMIT 0 , 30";

$get_entity_id = mysql_query($query1);

while($row = mysql_fetch_array($get_entity_id, MYSQL_ASSOC))
{
  $entity_id = $row['entity_id'];

  echo "  <div style='margin:30px 0px;'>
      entity_id: $entity_id<br />
    </div>
  ";
}

$query2 = "SELECT * FROM `sales_flat_quote_payment` WHERE `quote_id` = " . $entity_id . " LIMIT 0 , 30";

$get_additional_data = mysql_query($query2);

while($row = mysql_fetch_array($get_additional_data, MYSQL_ASSOC))
{
  $additional_data = $row['additional_data'];

  echo "  <div style='margin:30px 0px;'>
      additional_data: $additional_data<br />
    </div>
  ";
}

  echo "  <div style='margin:30px 0px;'>
      fee: $additional_data->transactions->fee<br />
    </div>
  ";

mysql_close($con);

我仍然无法从此阵列中检索我想要的数据。

我想要检索array(6) { ["component_mode"]=> string(4) "ebay" ["payment_method"]=> string(6) "PayPal" ["channel_order_id"]=> string(9) "123654789" ["channel_final_fee"]=> float(3.6) ["transactions"]=> array(1) { [0]=> array(4) { ["transaction_id"]=> string(15) "987456321456987" ["sum"]=> float(45) ["fee"]=> float(1.16) ["transaction_date"]=> string(19) "2016-03-31 05:43:43" } } ["tax_id"]=> string(0) "" }

我该怎么做?

1 个答案:

答案 0 :(得分:2)

它看起来像一个序列化数组。见unserialize() php doc

可以使用$additional_data['transactions'][0]["fee"]

检索费用