donglv5269 2018-08-28 08:43
浏览 87
已采纳

从php SESSION var中提取数据

I have a rather complex SESSION data set up in my code.. it contains variables and json data. What I would like to know is how to output a specific entry "name". It should return "John Smith" but currently it is returning nothing

ps. I should have said, this is NOT my code, its a wordpress plugin and I just want to pull the data they have provided in the SESSION var.

I've tried

print_r($_SESSION['asp_data']->source->name);
print_r($_SESSION['asp_data']['source']->name);
print_r($_SESSION['asp_data']['source']['name'];

and other variations of the above..

here's a var_dump of _SESSION

Array ( [asp_data] => Array ( [product_id] => 73134 [is_live] => 0 [item_name] => IQ Test [stripeToken] => tok_1D42CiLqRQ0yhU0NoaKBXisf [stripeTokenType] => card [stripeEmail] => spam@spammer.com [item_quantity] => 1 [item_price] => 19.99 [discount_item_price] => 19.99 [paid_amount] => 19.99 [amount_in_cents] => 1999 [currency_code] => USD [charge_description] => 1 X $19.99 [addonName] => [button_key] => [charge] => Stripe\Charge JSON: { "id": "ch_1D42CmLqRQ0yhU0NT3f0Zr4N", "object": "charge", "amount": 1999, "amount_refunded": 0, "application": null, "application_fee": null, "balance_transaction": "txn_1D42CmLqRQ0yhU0NHwqH2M9Y", "captured": true, "created": 1535443540, "currency": "usd", "customer": null, "description": "1 X $19.99", "destination": null, "dispute": null, "failure_code": null, "failure_message": null, "fraud_details": [], "invoice": null, "livemode": false, "metadata": [], "on_behalf_of": null, "order": null, "outcome": { "network_status": "approved_by_network", "reason": null, "risk_level": "normal", "seller_message": "Payment complete.", "type": "authorized" }, "paid": true, "receipt_email": "spam@spammer.com", "receipt_number": null, "refunded": false, "refunds": { "object": "list", "data": [], "has_more": false, "total_count": 0, "url": "\/v1\/charges\/ch_1D42CmLqRQ0yhU0NT3f0Zr4N\/refunds" }, "review": null, "shipping": null, "source": { "id": "card_1D42CiLqRQ0yhU0NeYtNArFt", "object": "card", "address_city": "London", "address_country": "United Kingdom", "address_line1": "Whitacres Road", "address_line1_check": "pass", "address_line2": null, "address_state": "SCT", "address_zip": "NW1 4DJ", "address_zip_check": "pass", "brand": "Visa", "country": "US", "customer": null, "cvc_check": "pass", "dynamic_last4": null, "exp_month": 2, "exp_year": 2022, "fingerprint": "AROHy31e67u0TucD", "funding": "credit", "last4": "4242", "metadata": [], "name": "John Smith", "tokenization_method": null }, "source_transfer": null, "statement_descriptor": null, "status": "succeeded", "transfer_group": null } [txn_id] => ch_1D42CmLqRQ0yhU0NT3f0Zr4N [billing_address] => John Smith Whitacres Road NW1 4DJ London SCT United Kingdom [shipping_address] => [additional_items] => Array ( ) [order_post_id] => 73150 [item_url] => [charge_date_raw] => 1535443540 [charge_date] => 2018/08/28 08:05:40 ) [score] => 91 )

here's the output of FOR EACH on _SESSION variable -

product_id 73134
is_live 0
item_name IQ Test
stripeToken tok_1D42CiLqRQ0yhU0NoaKBXisf
stripeTokenType card
stripeEmail spam@spammer.com
item_quantity 1
item_price 19.99
discount_item_price 19.99
paid_amount 19.99
amount_in_cents 1999
currency_code USD
charge_description 1 X $19.99
addonName 
button_key 
charge Stripe\Charge JSON: { "id": "ch_1D42CmLqRQ0yhU0NT3f0Zr4N", "object": 
"charge", "amount": 1999, "amount_refunded": 0, "application": null, 
"application_fee": null, "balance_transaction": 
"txn_1D42CmLqRQ0yhU0NHwqH2M9Y", "captured": true, "created": 1535443540, 
"currency": "usd", "customer": null, "description": "1 X $19.99", 
"destination": null, "dispute": null, "failure_code": null, 
"failure_message": null, "fraud_details": [], "invoice": null, "livemode": 
false, "metadata": [], "on_behalf_of": null, "order": null, "outcome": { 
"network_status": "approved_by_network", "reason": null, "risk_level": 
"normal", "seller_message": "Payment complete.", "type": "authorized" }, 
"paid": true, "receipt_email": "spam@spammer.com", "receipt_number": null, 
"refunded": false, "refunds": { "object": "list", "data": [], "has_more": 
false, "total_count": 0, "url": 
"\/v1\/charges\/ch_1D42CmLqRQ0yhU0NT3f0Zr4N\/refunds" }, "review": null, 
"shipping": null, "source": { "id": "card_1D42CiLqRQ0yhU0NeYtNArFt", 
"object": "card", "address_city": "London", "address_country": "United 
Kingdom", "address_line1": "Whitacres Road", "address_line1_check": "pass", 
"address_line2": null, "address_state": "SCT", "address_zip": "NW1 4DJ", 
"address_zip_check": "pass", "brand": "Visa", "country": "US", "customer": 
null, "cvc_check": "pass", "dynamic_last4": null, "exp_month": 2, 
"exp_year": 2022, "fingerprint": "AROHy31e67u0TucD", "funding": "credit", 
"last4": "4242", "metadata": [], "name": "John Smith", 
"tokenization_method": null }, "source_transfer": null, 
"statement_descriptor": null, "status": "succeeded", "transfer_group": null 
}
txn_id ch_1D42CmLqRQ0yhU0NT3f0Zr4N
billing_address John Smith Whitacres Road NW1 4DJ London SCT United Kingdom 
shipping_address 
additional_items Array
order_post_id 73150
item_url 
charge_date_raw 1535443540
charge_date 2018/08/28 08:05:40
  • 写回答

1条回答 默认 最新

  • dsmupo6631 2018-08-28 09:00
    关注

    You need to parse the json object first before you can read it using php, something like:

    // parse the json string value of the 'charge' key to make it a php array
    $json = json_decode($_SESSION['asp_data']['charge'], true);
    // echo the value of the 'name' key from the parsed json object
    echo $json['source']['name'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化