Hello,

I'm currently working on a project and I need to get the values I need from the API server.

I'm really new to working with objects and arrays alltogheter, can I have a little help and perhaps someone can provide a simple example on how I can do this ?

The code I get when quering the API server is as follow:

xmlrpcresp Object
(
    [val] => xmlrpcval Object
        (
            [me] => Array
                (
                    [array] => Array
                        (
                            [0] => xmlrpcval Object
                                (
                                    [me] => Array
                                        (
                                            [struct] => Array
                                                (
                                                    [id] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => numberIdOfItem
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                    [name] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => nameOfGroupItem
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                    [origin] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => ItemDetail1
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                    [vhost] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => ItemDetail2
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                    [ip] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => 3518896588
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                    [compression] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => 1
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                    [vanity_ip] => xmlrpcval Object
                                                        (
                                                            [me] => Array
                                                                (
                                                                    [string] => IPADDRESS
                                                                )

                                                            [mytype] => 1
                                                            [_php_class] => 
                                                        )

                                                )

                                        )

                                    [mytype] => 3
                                    [_php_class] => 
                                )

                        )

                )

            [mytype] => 2
            [_php_class] => 
        )

    [valtyp] => xmlrpcvals
    [errno] => 0
    [errstr] => 
    [payload] => 
    [hdrs] => Array
        (
            [server] => nginx
            [date] => Sun, 26 Aug 2012 10:19:27 GMT
            [content-type] => text/xml; charset=utf-8
            [transfer-encoding] => chunked
            [connection] => keep-alive
            [set-cookie] => PHPSESSID=bq1hp6ltjqqd6o8oo9el1otfc6; path=/
            [expires] => Thu, 19 Nov 1981 08:52:00 GMT
            [cache-control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
            [pragma] => no-cache
        )

    [_cookies] => Array
        (
            [PHPSESSID] => Array
                (
                    [value] => bq1hp6ltjqqd6o8oo9el1otfc6
                    [path] => /
                )

        )

    [content_type] => text/xml
    [raw_data] => HTTP/1.1 200 OK
Server: nginx
Date: Sun, 26 Aug 2012 10:19:27 GMT
Content-Type: text/xml; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: PHPSESSID=bq1hp6ltjqqd6o8oo9el1otfc6; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

I know how to get the values from arrays and objects but only from simple ones, so I'm not sure how I can do to get "numberIdOfItem","nameOfGroupItem","ItemDetail1","ItemDetail2".

Also this would be a list of items, so I belive I really need to use a loop to get what I need from there.

Can I please have some help to start on with this ? At least how to get the values from the arrays in the above objects.

Thank you in adavance!

If it is an object, you need to use -> to access the property, if it is an array use [] (either with index, or string). So in your case you need to mix them up.

$numberIdOfItem = $your_object->val->me[0][0]->me['struct']['id']->me['string']; // I think

I suggest you try it one step at a time.

Thank you for replying so fast, unfortunately using that I would get:

Notice: Trying to get property of non-object in /home/user/Desktop/www/api/get.php on line 149

Notice: Trying to get property of non-object in /home/user/Desktop/www/api/get.php on line 149

Notice: Trying to get property of non-object in /home/user/Desktop/www/api/get.php on line 149

Notice: Trying to get property of non-object in /home/user/Desktop/www/api/get.php on line 149

where the code I use is:

$numberIdOfItem = $test->val->me[0][0]->me['struct']['id']->me['string']; 
echo 'result:' . $numberIdOfItem;

and $test contains the above result query from the api.

Told you to take one step at the time. Start with print_r($test->val) and go from there.

Sorry, indeed you were right on this. It was a little different then what you provided, but almost the same, was an array element instead of the [0]:

print_r($r->val->me['array'][0]->me['struct']['origin']->me['string']);

Do you have a suggestion on how I should create the loop on this one ?

As I said, I only need those 4 elements from the object arrays but this is a list of items.

Thank you again!

I'll reply here to let you know I'm good now :)

//print_r($r);
//$numberIdOfItem = $test->val->me[0][0]->me['struct']['id']->me['string'];
$unu = $r->val->me['array'];
//print_r($unu);

foreach ($unu as &$value)
{
    $string = $value->me['struct']['id']->me['string'];
    echo $string;
    echo "<br/>";
}

and the result is
IDnumber
IDNumber

The same I believe it can be done with the others.
Thanks you for you help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.