PHP arrays

View: New views
3 Messages — Rating Filter:   Alert me  

PHP arrays

by Andres Gonzalez-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am currently using SWIG to implement a PHP interface to a C/C++
application. In all of my C/C++ API functions, I simply return a
string with delimiters, for example, I would return something
like this:

sprintf(buffer, "%d\n%d\n%s\n%d", intParam1, intParam2, strParam3,
intParam3);
return buffer;

When my PHP application uses this API function, it gets a string so then
I use the following to put it in an array for use in the PHP domain:

$ret = apiFuncition();
$myArray = explode(PHP_EOL, $ret);

This is working very well, however, I now need to have my C/C++ functions
return more complex associative arrays, for example like this:

"key1"        [0] = value1
                   [1] = value2
                   [2] = value3
"key2"         [0] = value4
                   [1] = value5
"key3"         [0] = value6
                   [1] = value7

That is, an array that has an array as elements.

Does SWIG support this?  How do I format my data in the C/C++
domain so that I can get associative arrays in the PHP domain?

Thanks,

-Andres




------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: PHP arrays

by Miklos Vajna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 23, 2009 at 10:01:34AM -0400, Andres Gonzalez <andres@...> wrote:
> Does SWIG support this?  How do I format my data in the C/C++
> domain so that I can get associative arrays in the PHP domain?

I haven't tried it myself, but looking at the contents of the Lib/php
directory, I would try just returning an
std::map<int,std::vector<std::string>>.


------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

attachment0 (204 bytes) Download Attachment

Re: PHP arrays

by Olly Betts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2009-06-23, Miklos Vajna <vmiklos@...> wrote:
> On Tue, Jun 23, 2009 at 10:01:34AM -0400, Andres Gonzalez <andres@...> wrote:
>> Does SWIG support this?  How do I format my data in the C/C++
>> domain so that I can get associative arrays in the PHP domain?
>
> I haven't tried it myself, but looking at the contents of the Lib/php
> directory, I would try just returning an
> std::map<int,std::vector<std::string>>.

Another approach is to write a SWIG typemap which builds the PHP associative
array using the PHP C API.  I don't have an example for associative arrays to
hand, but here is one which returns a PHP array for a C function which
returns a pair of C++ iterators:

%typemap(out) std::pair<Xapian::TermIterator, Xapian::TermIterator> {
    if (array_init($result) == FAILURE) {
        SWIG_PHP_Error(E_ERROR, "array_init failed");
    }

    for (Xapian::TermIterator i = $1.first; i != $1.second; ++i) {
        /* We have to cast away const here because the PHP API is rather
         * poorly thought out - really there should be two API methods
         * one of which takes a const char * and copies the string and
         * the other which takes char * and takes ownership of the string.
         *
         * Passing 1 as the last parameter of add_next_index_stringl() tells
         * PHP to copy the string pointed to by p, so it won't be modified.
         */
        string term = *i;
        char *p = const_cast<char*>(term.data());
        add_next_index_stringl($result, p, term.length(), 1);
    }
}

Cheers,
    Olly


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user