Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:02:47+00:00 2026-06-12T00:02:47+00:00

I’m trying to return an associative array in a PHP function. This function retrieves

  • 0

I’m trying to return an associative array in a PHP function. This function retrieves some data from MySQL and is published in a WebService using Zend Library.

This function looks like:

Functions.php

class Functions  
{
    /** 
    *
    * @return array 
    */  
    public function Function1()  
    {

        $con = new mysqli(server, user, pass, database);

        if ($con->connect_errno) {
            die(__FUNCTION__ . $con->connect_error);
        }


        $con->set_charset('utf8');
        $arrayRet = array();

        $query = 'select field1, field2, field3 from table1';

        if(!$result = $con->query($query)){
            die(__FUNCTION__ . $con->error);
        }

        while( $row = $result->fetch_array(MYSQLI_ASSOC) )
            $arrayRet[] = $row;

        $result->close();
        $con->close(); 

        return $arrayRet;

    }
}

Then I have another PHP file that lets me access the function via SOAP (using Zend Lib):

Server.php

<?php
  include 'Zend/Soap/AutoDiscover.php';  
  include 'Zend/Soap/Server.php';  
  include 'Functions.php';  

  if(isset($_GET['wsdl']))   
  {  
     $autodiscover = new Zend_Soap_AutoDiscover();  
     $autodiscover->setClass('Functions');  
     $autodiscover->handle();  
  }   
  else   
  {  
     $server = new Zend_Soap_Server("http://localhost/webservice/Server.php?wsdl");  
     $server->setClass('Functions');  
     $server->handle();  
  }  
?>

So far everything looks quite normal. If i write a php client, i’m able to consume the webservice and access the returned array as follows:

<?php

$client = new Zend_Soap_Client('http://localhost/webservice/Server.php?wsdl');
$arrayRet = $client->Function1();

foreach($arrayRet as $row )  
{  
 ?>
    <b>Field1: </b><?php echo $row['field1']; ?><br>
    <b>Field2: </b><?php echo $row['field2']; ?><br>
    <b>Field3: </b><?php echo $row['field3']; ?><br>
<?php  
  }  
?>

Ok, now my problem is that i’m not writing a php client but a Android client. I’m using the ksoap2 library to achieve it. With this library i’m able to handle “normal arrays” with the pare key and value, following this algorithm. Using his properies:

    SoapObject category_list = (SoapObject) property;
    String key = category_list.getProperty("key").toString();
    String value = category_list.getProperty("value").toString();

But the response from the function above (if i copy the toString() result) looks like:

Function1Response
{
  return=
  [
   Map{
    item=anyType{key=field1; value=hello; }; 
    item=anyType{key=field2; value=web; }; 
    item=anyType{key=field3; value=service; };}, 

   Map{
    item=anyType{key=field1; value=hello2; }; 
    item=anyType{key=field2; value=web2; }; 
    item=anyType{key=field3; value=service2; };}, 

   Map{
    item=anyType{key=field1; value=hello3; }; 
    item=anyType{key=field2; value=web3; }; 
    item=anyType{key=field3; value=service3; };}
   ]; 
  }

I could iterate this response using key and value properties, but i think it would be much better (and efficient) if i could have a response like:

Function1Response
{
  return=
  [
   Map{
    item=anyType{field1=hello; }; 
    item=anyType{field2=web; }; 
    item=anyType{field3=service; };}, 

   Map{
    item=anyType{field1=hello2; }; 
    item=anyType{field2=web2; }; 
    item=anyTypefield3=service2; };}, 

   Map{
    item=anyType{field1=hello3; }; 
    item=anyType{field2=web3; }; 
    item=anyType{field3=service3; };}
   ]; 
  }

So i could retrive them like:

    SoapObject category_list = (SoapObject) property;
    String field1 = category_list.getProperty("field1").toString();
    String field2 = category_list.getProperty("field2").toString();
    String field3 = category_list.getProperty("field3").toString();

Is that possible? I think it can be done somehow in the php server side. But i have no idea. I have been reading here and there but nobody seems to have this problem.. or a solution.

I’m sorry for the long post. I could give more code details or explain it better if i’ve been not clear enough.

Thanks for helping!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:02:48+00:00Added an answer on June 12, 2026 at 12:02 am

    Well, i did find the solution.

    The function in the PHP server that will publish the result in the webservice looks like:

    class Functions  
    {
        /** 
        *
        * @return array 
        */  
        public function Function1()  
        { 
    
            $con = new mysqli(server, user, pass, database);
    
            if ($con->connect_errno) {
                die(__FUNCTION__ . $con->connect_error);
            }
    
    
            $con->set_charset('utf8');
            $arrayRet = array();
    
            $query = 'select field1, field2, field3 from table1';
    
            if(!$result = $con->query($query)){
                die(__FUNCTION__ . $con->error);
            }
    
            while( $row = $result->fetch_array(MYSQLI_ASSOC) ){
                $myClass = new TestClass();
                $myClass->field1 = $row["field1"];
                $myClass->field2 = $row["field2"];
                $myClass->field3 = $row["field3"]; 
                array_push($arrayRet, $myClass);
            }
    
    
            $result->close();
            $con->close(); 
    
            return $arrayRet;
    
        }
    }
    

    I have previously created a class with all relevant attributes, as simple as:

    TestClass.php

    <?php
    class TestClass 
    {
        /**
        * @var string 
        */
        public $field1;
    
        /**
        * @var string 
        */
        public $field2;
    
        /**
        * @var string 
        */
        public $field3;
    
    }
    
    ?>
    

    So basically we’re returning an array filed with all the different instances of this class as items.

    Now, in the Android Client Side (always using ksoap2 library) i receive a response like:

    Function1Response
    {
      return=
      [
        Struct{field1=hello; field2=web; field3=service;  },
        Struct{field1=hello2; field2=web2; field3=service2; }, 
        Struct{field1=hello3; field2=web3; field3=service3;}
      ]; 
    }
    

    and i’m able to iterate it:

            //call
            androidHttpTransport.call(SOAP_ACTION, envelope);
    
            //envelope response
            SoapObject result = (SoapObject) envelope.bodyIn;
    
            //retrieve the first property, actually the array
                    Vector result3 = (Vector) result.getProperty(0);
    
            //iterate
            Enumeration e=result3.elements(); e.hasMoreElements();)
            {
                //each object from the array its an Soap Element that contanins the properties defined in the php class
                SoapObject item=((SoapObject)e.nextElement());
    
                String field1 = item.getProperty("field1").toString();
                String field2 = item.getProperty("field2").toString();
                String field3 = item.getProperty("field3").toString();
    
                stringBuilder.append(
                        "Field1: " + codi + "\n"+ 
                        "Field2: "+ titol +"\n"+ 
                        "Field3: "+ descr +"\n"+
                        "******************************\n");
            }
    

    And that’s it… i hope it will be useful for someone else.
    Thanks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.