【问题标题】:PHP return object variables inside of an arrayPHP返回数组内的对象变量
【发布时间】:2012-04-25 17:28:03
【问题描述】:

我正在编写一个 PHP 脚本来处理一些 JSON 数据。以下是(删节的)var_dump($data)。我想返回与["[question(13), option(0)]"] 关联的值,即 20。我不知道该怎么做。我试过$data->[question(13), option(0)]$data->question(13)。 (我试着查了一下,但我不确定这个符号是什么意思,所以我不确定我在找什么)

object(stdClass)#133 (36) {
  ["id"]=>
  string(1) "1"
  ["contact_id"]=>
  string(0) ""
  ["status"]=>
  string(8) "Complete"
  ["is_test_data"]=>
  string(1) "0"
  ["datesubmitted"]=>
  string(19) "2012-04-19 17:11:00"
  ["[question(5)]"]=>
  string(11) "C.    40%, 40%"
  ["[question(9)]"]=>
  string(47) "D.    EBITDA and Free cash flow are the same thing"
  ["[question(10)]"]=>
  string(48) "A.    Accounts Payable as % of sales would increase"
  ["[question(11)]"]=>
  string(20) "E.    None of the above"
  ["[question(12)]"]=>
  string(97) "A.     A larger portion of initial investment is equity which can increase exit return potential."
  ["[question(13), option(0)]"]=>
  string(2) "20"
  ["[url("embed")]"]=>
  string(0) ""
  ["[variable("STANDARD_IP")]"]=>
  string(13) "38.107.74.230"
  ["[variable("STANDARD_LONG")]"]=>
  string(10) "-73.976303"
  ["[variable("STANDARD_LAT")]"]=>
  string(9) "40.761902"
}

【问题讨论】:

  • 你真的应该让你的属性遵循常见的变量名称约定。

标签: php arrays json multidimensional-array


【解决方案1】:

要么使用扩展对象访问表示法:

$data->{'[question(13), option(0)]'}

或者只是要求普通数组并将其用作普通数组。

json_decode($json, true);

【讨论】:

  • 啊……臭名昭著的大括号。谢谢!出于好奇,这个符号是什么意思?那些是对象的实例变量吗?
  • @Emerson:是的,他们是。更多信息请查看stackoverflow.com/questions/3737139/…
【解决方案2】:

试试这个

echo $data->{'[question(13), option(0)]'};

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2015-09-13
    • 2015-04-24
    • 2017-11-23
    相关资源
    最近更新 更多