从PHP数组响应中获取某些值

时间:2016-04-14 21:46:26

标签: php arrays

我正在尝试从数组中提取["details"],以便显示

Array Value 1=address
Array Value 2=no_match
Array Value 3=address_risk
Array Value 4=low

我尝试了以下操作,但它没有返回任何值

foreach ($person as $key => $val) {
    print "$key = $val\n";
}

所以我应该能够查询数组值if address="no_match" Print "no match" enter image description here

数组值$ person

object(TestScore\Person)#1 (2) {
  ["_values":protected]=>
  array(25) {
    ["id"]=>
    string(24) "57100c7832366100030011e0"
    ["object"]=>
    string(6) "person"
    ["created_at"]=>
    int(1460669560)
    ["updated_at"]=>
    int(1460669560)
    ["status"]=>
    string(5) "valid"
    ["livemode"]=>
    bool(false)
    ["phone_number"]=>
    NULL
    ["ip_address"]=>
    NULL
    ["birth_day"]=>
    int(1)
    ["birth_month"]=>
    int(1)
    ["birth_year"]=>
    int(1990)
    ["name_first"]=>
    string(4) "Jane"
    ["name_middle"]=>
    NULL
    ["name_last"]=>
    string(3) "Doe"
    ["address_street1"]=>
    string(17) "123 Something Ave"
    ["address_street2"]=>
    NULL
    ["address_city"]=>
    string(12) "Newton Falls"
    ["address_subdivision"]=>
    string(2) "OH"
    ["address_postal_code"]=>
    string(5) "44444"
    ["address_country_code"]=>
    string(2) "US"
    ["document_type"]=>
    string(3) "ssn"
    ["document_value"]=>
    string(4) "0000"
    ["note"]=>
    NULL
    ["details"]=>
    object(stdClass)#3 (6) {
      ["address"]=>
      string(8) "no_match"
      ["address_risk"]=>
      string(3) "low"
      ["identification"]=>
      string(8) "no_match"
      ["date_of_birth"]=>
      string(9) "not_found"
      ["ofac"]=>
      string(8) "no_match"
      ["pep"]=>
      string(8) "no_match"
    }
    ["question_sets"]=>
    object(TestScore\QuestionSet)#4 (3) {
      ["_existing":"TestScore\QuestionSet":private]=>
      array(0) {
      }
      ["_values":protected]=>
      array(1) {
        ["id"]=>
        string(24) "57100c7832366100030011e0"
      }
      ["_unsavedValues":protected]=>
      NULL
    }
  }
  ["_unsavedValues":protected]=>
  array(0) {
  }
}
NULL

2 个答案:

答案 0 :(得分:1)

:此:

foreach ($person as $key => $val) {
    print "$key = $val\n";
}

应该是:

foreach ($person["details"] as $key => $val) {
    print "$key = $val\n";
}

<强>原因:
要从嵌套的数组中提取数组,您需要放大以类似于文件系统,其中差别为[first_array] [second_array] ...

答案 1 :(得分:0)

问题是_values属性受到保护,并且在从类外部迭代时不可见。有关访问受保护资产的几种方法的解释,请参阅此问题:

How to get protected property of object in PHP