一千萬個為什麽

搜索

如何在PHP中訪問此數據的成員?

好的。現在我放棄了。我已經玩了好幾個小時。

  • I have a variable name $data.
  • The variable contains these contents: (extracted by using var_export())

    array (
      'headers' =>
      array (
        'content-type' => 'multipart/alternative; boundary="_689e1a7d-7a0a-442a-bd6c-a1fb1dc2993e_"',
      ),
      'ctype_parameters' => 
      array (
        'boundary' => '_689e1a7d-7a0a-442a-bd6c-a1fb1dc2993e_',
      ),
      'parts' => 
      array (
        0 => 
        stdClass::__set_state(array(
          'headers' => 
          array (
            'content-type' => 'text/plain; charset="iso-8859-1"',
            'content-transfer-encoding' => 'quoted-printable',
          ),
          'ctype_primary' => 'text',
        )),
      ),
    )
    

    I removed some non-essential data.

  • I want to access the headers value (on the second line above) - simple: $data->headers
  • I want to access the headers value (on the fourteenth line after the stdClass:: stuff) - how?

我怎樣才能訪問 stdClass :: __ set_state 部分中的值?

I tried var_export($data->parts); but all I get is

NULL

最佳答案

此變量是否以您發布的方式聲明?喜歡:

$data = array(
          'headers' =>
             array (
             …

In that case, I'm not quite sure how you can access 'headers' via $data->headers. It should be $data['headers'], because it is an array, not an object.

Further down, stdClass::__set_state(array('headers' => …)) statically calls the method __set_state of the class stdClass. Whatever this method does I don't know, but only its return value will be assigned to the 'parts' => array(0 => ...) key.

如果OTOH你所展示的是 var_dump($ data)的結果,那麽這是不正確的廢話,因為 stdClass :: __ set_state()永遠不會顯示在 var_dump 中。在您的代碼或您發布的內容中,有些東西是可疑的,如果沒有看到更多內容,很難說。

忽略上面的內容, var_export 以這種時髦的方式打印類。

$data['headers'] should do it for the first headers part. Further down, $data['parts'][0]->headers should do the trick.

轉載註明原文: 如何在PHP中訪問此數據的成員?