我想從simpleXmlElement構建一個新數組。我收到我想要的信息,只是沒有在正確的層次結構中。從其他陣列構建陣列
$xmlNew1 = new SimpleXMLElement($responseNew1);
$test = array();
foreach ($xmlNew1->children() as $newChild){
$classIden[] = (string)$xmlNew1->class['id'];
$item[] = (string)$xmlNew1->code;
$family[] = (string)$xmlNew1->family;
for($i=0, $count = count($classIden); $i < $count; $i++) {
$test[$item[$i]][$family[$i]]= $classIden[$i];
}
}
print_r($test);
這給了我:
Array
(
[9522] => Array
(
[Mens Hats] => 44
)
[9522-NC-NO SIZE] => Array
(
[Mens Hats] => 44
)
[B287CSQU] => Array
(
[Boys] => 1
)
,但我想
Array
(
[9522] => Array
(
[family] => Mens Hats
[classId] => 44
)
什麼建議嗎?謝謝!
我不需要在foreach摳每一個孩子叫出來? – 2012-08-14 04:30:30
@TylerNichol更新了我的代碼。 – 2012-08-14 04:38:47