日期:2014-05-17  浏览次数:20466 次

PHP取Webservice数据返回数组维度不正确
我用nusoap插件读取webservice,但是出现了问题。
当返回的数组大于1组时,返回正确,但是当数组数据只有1组是,维度就发生了变化,导致count获取数组长度就不正确。
代码如下:
当有2组数据时,结果如下:
PHP code

    Array
    (
        [getTrafficResult] => Array
            (
                [trafficWay] => Array
                    (
                        [0] => Array
                            (
                                [Station] => 4828,4830,4868
                                [Line] => 2908,2906
                                [Price] => 10.5
                                [Alltime] => 10
                                [ChangeCount] => 1
                                [Type] => 2,2
                                [Station_name] => 尖沙咀地铁站#金钟地铁站#铜锣湾地铁站
                                [Line_name] => 荃湾线,港岛线
                                [Allstation] => 0|湾仔地铁站
                                [Stationcount] => 1,2
                                [Direction] => 中环地铁站#柴湾地铁站
                                [Time] => 5,5
                                [Distance] => 2172,2017
                                [Lineremark] => 0#0
                                [Addstartinfo] => 0
                                [Addendinfo] => 0
                            )
                        [1] => Array
                            (
                                [Station] => 2655,2847
                                [Line] => 1084
                                [Price] => 9.3
                                [Alltime] => 30
                                [ChangeCount] => 0
                                [Type] => 1
                                [Station_name] => 北京道,广东道#维多利亚公园,告士打道
                                [Line_name] => 新巴110
                                [Allstation] => 新世界中心,梳士巴利道#加连威老道,漆咸道南#香港科学馆,漆咸道南#牲口站,漆咸道北#海底隧道收费广场
                                [Stationcount] => 6
                                [Direction] => 筲箕湾
                                [Time] => 20
                                [Distance] => 2552
                                [Lineremark] => 0
                                [Addstartinfo] => 334#5#0
                                [Addendinfo] => 337#5#0
                            )
                    )
            )
    )



但是当只有一组数据时,就少了一维:

PHP code

    Array
    (
        [getTrafficResult] => Array
            (
                [trafficWay] => Array
                    (
                        [Station] => 4828,4827,4823,4855,4573
                        [Line] => 3190,2914,4227,3730
                        [Price] => 8
                        [Alltime] => 28
                        [ChangeCount] => 1
                        [Type] => 5,2,2,5
                        [Station_name] => 尖沙咀地铁站#尖东地铁站#红磡地铁站#沙田地铁站#新城市广场
                        [Line_name] => 步行,西铁线,东铁线,步行
                        [Allstation] => 0|0|旺角东地铁站#九龙塘地铁站#大围地铁站|0
                        [Stationcount] => 0,1,4,0
                        [Direction] => 0#红磡地铁站#落马洲地铁站#0
                        [Time] => 6,3,15,2
                        [Distance] => 360,1125,8810,163
                        [Lineremark] => 0#0#0#从A出口
                        [Addstartinfo] => 0
                        [Addendinfo] => 0
                    )
            )
    )



我用$arr['getTrafficResult']['trafficWay']取数据并取得长度,第一个获取长度为2,正确,但是第二个获取就成了16,成了 字段的个数了。

请问这个问题如何解决

------解决方案--------------------
从例子看,你只需判断 isset($arr['getTrafficResult']['trafficWay'][0])即可区分 <