這裏是我的物體看起來像print_r的(這是由PHP SDK爲亞馬遜Web服務的簡單數據庫返回的對象。循環訪問的SimpleXML Objext PHP
[GetAttributesResult] => CFSimpleXML Object
(
[Attribute] => Array
(
[0] => CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
[1] => CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
[2] => CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
[3] => CFSimpleXML Object
(
[Name] => data_title
[Value] => Company Info
)
[4] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => firsttag
)
[5] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => secondtag
)
[6] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => thirdtag
)
[7] => CFSimpleXML Object
(
[Name] => data_files
[Value] => company_info.flv
)
[8] => CFSimpleXML Object
(
[Name] => data_id
[Value] => 8993
)
)
)
我有一個功能,在迭代GetAttributesResult對象並創建一個關聯數組,使得通過名稱引用我的字段變得很容易,我的一個名字是data_tags,它重複了未知次數,我想返回data_tags作爲這些值的簡單索引數組。這是我的功能,不起作用。
function attrToArray($select) {
$results = array();
$x = 0;
foreach($select->body->GetAttributesResult as $result) {
foreach ($result as $field) {
if (array_key_exists($field,$results[$x])) {
$results[$x][ (string) $field->Name ][] = (string) $field->Value;
} else {
$results[$x][ (string) $field->Name ] = (string) $field->Value;
}
}
$x++;
}
return $results;
}
我不知道這是否是最優雅的解決方案,但我不明白爲什麼它不起作用。 array_key_exists不返回true。由於錯誤,我能夠測試in_array($field-Name,$results[$x])
,並構建了我重複的$ field-> Name值的數組......但它也將所有其他值轉換爲單個項目嵌套數組...因此它似乎返回真的比我想象的還要多。儘管那裏的連字符是我錯誤地使用的 - >哪個不會返回true,所以我對那裏發生的事情感到困惑。這是print_r來顯示返回的內容。
Array ([0] => Array (
[data_datein] => 2011-04-23
[data_estatus] => 0
[data_status] => Array ([0] => 1)
[data_title] => Array ([0] => Company Info)
[data_tags] => Array (
[0] => firsttag
[1] => secondtag
[2] => thirdtag)
[data_files] => Array ([0] => company_info.flv)
[data_id] => Array ([0] => 8993)))
上我可能會如何處理這更好的...而且最起碼,如果有人可以找出任何指針,建議或指導我怎樣才能到上述陣列而對其他非冗餘的嵌套數組領域。非常感謝!
這裏是$result
的print_r()
CFSimpleXML對象 ( [屬性] =>數組 ( [0] => CFSimpleXML對象 ( [名稱] => data_datein [數值] => 2011-04 -23 )
[1] => CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
[2] => CFSimpleXML Object
(
[Name] => data_title
[Value] => 0001 01 Company Name
)
[3] => CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
[4] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => good stuff
)
[5] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => save tags
)
[6] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => tagger works
)
[7] => CFSimpleXML Object
(
[Name] => data_files
[Value] => 0001_01_company_name.flv
)
[8] => CFSimpleXML Object
(
[Name] => data_id
[Value] => yFKwIxjIhH
)
)
)
和這裏是$field
(迭代和由<hr>
標籤分離的print_r()
。)
CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
<hr>CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
<hr>CFSimpleXML Object
(
[Name] => data_title
[Value] => 0001 01 Company Name
)
<hr>CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => good stuff
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => save tags
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => tagger works
)
<hr>CFSimpleXML Object
(
[Name] => data_files
[Value] => 0001_01_company_name.flv
)
<hr>CFSimpleXML Object
(
[Name] => data_id
[Value] => yFKwIxjIhH
)
我喜歡simplexml!這是最好的=) – Rudie 2011-04-25 12:52:52
你可以添加$ result和$ field的print_r嗎? – Eugene 2011-04-25 12:56:59