0

I need to create an XML-RPC server that gets cities with their corresponding IDs. What I do as a response is looking weird to me because of unnecessary duplicate entries but I couldnt find a better way.

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

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

    )

I implemented above response. With this implementation:

$response = array(
                        array(

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

                        'struct'
                );

The problem is I want to take a response like this :

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

So I tried to implement it like this :

$response = array(
                        array(

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

                        'struct'
                );

But it fails with this implementation. What am I doing wrong ?

Thanks

dreamend
  • 59
  • 4
  • 19

2 Answers2

0

You have array like following

$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);

You will be getting the expected array.

Array
(
    [cities] => Array
        (
            [34] => Istanbul
            [35] => Izmir
            [6] => Ankara
        )
)
Raja
  • 600
  • 6
  • 15
  • Thanks but the problem is: How can I implement an array like the below one on response array. I know I can make a foreach to create a new array, but this is not usable as response. – dreamend Apr 02 '15 at 12:49
0

This is how I do it, in 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'));