簡體   English   中英

從PHP數組響應中獲取某些值

[英]Fetch Certain Values from a PHP Array Response

我試圖從數組中提取["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"我應該能夠查詢Array值 在此處輸入圖片說明

數組值$ 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

這個:

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

應該:

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

原因:
要從嵌套的數組中提取數組,您需要放大類似於文件系統,其中的區別是[first_array] [second_array] ...

問題是_values屬性受到保護,並且從類外部進行迭代時不可見。 有關無論如何訪問受保護屬性的幾種方式的說明,請參見以下問題:

如何在PHP中獲取對象的受保護屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM