Codeigniter XML-RPC响应多维数组方法

时间:2015-04-01 11:14:41

标签: php arrays xml codeigniter xml-rpc

我需要创建一个XML-RPC服务器,以获取具有相应ID的城市。作为回应我做的事情看起来很奇怪,因为不必要的重复条目,但我找不到更好的方法。

 Array
    (
        [cityID] => Array
            (
                [0] => 34
                [1] => 35
                [2] => 06
            )

        [cityName] => Array
            (
                [0] => Istanbul
                [1] => Izmir
                [2] => Ankara
            )

    )

我实施了以上回复。有了这个实现:

$response = array(
                        array(

                                'cityID' => array(array('34', '35', '06'), 'array'),
                                'cityName' => array(array('Istanbul', 'Izmir', 'Ankara'), 'array')
                        ),

                        'struct'
                );

问题是我想采取这样的回应:

Array
       (
         [cities] => Array
             (
                  ['34'] => 'Istanbul'
                  ['35'] => 'Izmir'
                  ['06'] => 'Ankara'
             )
       )

所以我试着像这样实现它:

$response = array(
                        array(

                                'cities' => array(array('34'=>'Istanbul', '35'=>'Izmir', '06'=>'Ankara'), 'array')
                        ),

                        'struct'
                );

但是这个实现失败了。我究竟做错了什么 ?

谢谢

2 个答案:

答案 0 :(得分:0)

你有像下面这样的数组

$response =  array ( 'cityID' => array (
                                          0 => 34,
                                          1 => 35,
                                          2 => 06
                                        ), 

                    'cityName' => array(  
                                          0 => 'Istanbul',
                                          1 => 'Izmir',
                                          2 => 'Ankara'
                                        )
                );

    $newarray = array();

    foreach($response['cityID'] as $key => $cityid){
        $newarray['cities'][$cityid] = $response['cityName'][$key];
    }

    print_r($newarray);

您将获得预期的阵列。

Array
(
    [cities] => Array
        (
            [34] => Istanbul
            [35] => Izmir
            [6] => Ankara
        )
)

答案 1 :(得分:0)

这就是我在Code Igniter 3中的表现

$array =  array ( 'cityID' => array (
                                      0 => 34,
                                      1 => 35,
                                      2 => 06
                                    ), 

                'cityName' => array(  
                                      0 => 'Istanbul',
                                      1 => 'Izmir',
                                      2 => 'Ankara'
                                    )
            );

foreach($array['cityID'] as $key => $cityid){
     $response[] = array(array(
           $cityid => array($array['cityName'][$key],'string'),
    ),'struct');
}

return $this->xmlrpc->send_response(array($response,'array'));