我试图从一个复杂的关联数组中获取它从xml文件中获取的值。这是数组的实际输出(https://www.dropbox.com/s/k6lxrx6h9x3x83u/xml-first.txt?dl=0)。
这是一个示例
Array
(
[drawserialnumber] => 48285
[lotteryname] => Akshaya
[shortname] => AK
[drawdate] => 2015-06-17
[drawheld] => Array
(
)
[drawnumber] => 194
[drawvenew] => SREE CHITHRA HOME AUDITORIUM, PAZHAVANGADI, EAST FORT, THIRUVANANTHAPURAM
[approvedby] => A.Jayakumar
[approverdesig] => Deputy Director
[approveroffice] => Directorate Of State Lotteries , Vikas Bhavan,tvm
[prizes] => Array
(
[prizedetails] => Array
(
[0] => Array
(
[prizeno] => 1
[prizedesc] => Rs :6,500,000/-
[totalamt] => 6500000
[conslation] => f
[prizeticket] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
)
[1] => Array
(
[prizeno] => 1
[prizedesc] => Rs :10,000/-
[totalamt] => 10000
[conslation] => t
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AJ
[digit] => 279045
[district] => NIL
)
[1] => Array
(
[seriesname] => AR
[digit] => 279045
[district] => NIL
)
[2] => Array
(
[seriesname] => AM
[digit] => 279045
[district] => NIL
)
[3] => Array
(
[seriesname] => AO
[digit] => 279045
[district] => NIL
)
[4] => Array
(
[seriesname] => AP
[digit] => 279045
[district] => NIL
)
[5] => Array
(
[seriesname] => AL
[digit] => 279045
[district] => NIL
)
)
)
[2] => Array
(
[prizeno] => 2
[prizedesc] => Rs :200,000/-
[totalamt] => 200000
[conslation] => f
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AR
[digit] => 420734
[district] => KANNUR
)
[1] => Array
(
[seriesname] => AJ
[digit] => 221136
[district] => ALAPPUZHA
)
[2] => Array
(
[seriesname] => AK
[digit] => 825429
[district] => WAYANAD
)
[3] => Array
(
[seriesname] => AL
[digit] => 171621
[district] => THIRUVANANTHAPURAM
)
[4] => Array
(
[seriesname] => AM
[digit] => 786170
[district] => ERNAKULAM
)
[5] => Array
(
[seriesname] => AO
[digit] => 668158
[district] => PALAKKAD
)
[6] => Array
(
[seriesname] => AP
[digit] => 244326
[district] => KANNUR
)
)
)
[3] => Array
(
[prizeno] => 3
[prizedesc] => Rs :10,000/-
[totalamt] => 10000
[conslation] => f
[prizeticket] => Array
(
[seriesname] => Array
(
)
[digit] => 25426
[district] => Array
(
)
)
)
[4] => Array
(
[prizeno] => 4
[prizedesc] => Rs :5,000/-
[totalamt] => 5000
[conslation] => f
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => Array
(
)
[digit] => 6989
[district] => Array
(
)
)
[1] => Array
(
[seriesname] => Array
(
)
[digit] => 3242
[district] => Array
(
)
)
等等可能会出现
现在我想将详细信息发送到html表,它几乎已经完成,但问题是
pricedetails
数组。
以下是获取pricedetails
foreach ($data['prizes'] as $prizes) {
foreach($prizes as $prize){
echo '<pre>';
print_r($prize);
echo '</pre>';
$temp=$prize['prizeticket'];
foreach($temp as $ticket){
print_r($ticket);
}
}
}
以下是Print_r($prize)
Array
(
[prizeno] => 1
[prizedesc] => Rs :6,500,000/-
[totalamt] => 6500000
[conslation] => f
[prizeticket] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
)
Array
(
[prizeno] => 1
[prizedesc] => Rs :10,000/-
[totalamt] => 10000
[conslation] => t
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AJ
[digit] => 279045
[district] => NIL
)
[1] => Array
(
[seriesname] => AR
[digit] => 279045
[district] => NIL
)
[2] => Array
(
[seriesname] => AM
[digit] => 279045
[district] => NIL
)
[3] => Array
(
[seriesname] => AO
[digit] => 279045
[district] => NIL
)
[4] => Array
(
[seriesname] => AP
[digit] => 279045
[district] => NIL
)
[5] => Array
(
[seriesname] => AL
[digit] => 279045
[district] => NIL
)
)
)
如您所见,第一次迭代在prizedetails
下仅包含一个子数组,第二次迭代在prizedetails
下包含多个子数组。
问题是我无法通过两个键值对获得值。
print_r($ticket)
输出
AK
279045
THRISSUR
Array
(
[seriesname] => AJ
[digit] => 279045
[district] => NIL
)
Array
(
[seriesname] => AR
[digit] => 279045
[district] => NIL
)
请注意,第一个输出AK
279045
THRISSUR
打印没有任何键,它像文本一样,其余的输出正确,但我需要打印所有的数组键。我知道这是因为{{1}第一次迭代在print_r($prize)
下只包含一个子数组。我该如何解决呢?
更新 解析xml的代码
prizedetails
答案 0 :(得分:0)
您的代码表现正常。您需要更改代码,以便&#34; IF&#34; prizeticket有一个项目,它做一件事,&#34; ELSE&#34;它做了别的事。
if(!isset($prize['prizeticket'][0])) // It has one item
print_r($prize['prizeticket']);
else foreach($prize['prizeticket'] as $ticket)
print_r($ticket);
这是使用功能的好地方。假设它被命名为`handle_a_ticket($ ticket)&#39;,你将用handle_a_ticket替换print_r。
答案 1 :(得分:0)
我的第一反应是XML有拼写错误。
List<? extends Animal>
应该看起来像:
[prizeticket] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
如果您有一些要求绝对必须保留XML,我建议检查索引0的存在,如下所示:
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
答案 2 :(得分:0)
最后,我找到了基于@ RiggsFolly评论的解决方案。
$xml=simplexml_load_file('AK-194.xml');
foreach($xml->prizes as $prize){
foreach($prize->prizedetails as $details){
foreach($details->prizeticket as $tickets){
print_r($tickets);
}
}
}