0

I'm coding a login system using API. i'm stuck with the result that i got using the API. I got the result as normal string like this:

stdClass Object
(
    [success] => 1
    [message] => Array
        (
            [code] => 000
            [txt] => Operation successful
        )

    [data] => Array
        (
            [result] => OK
            [accountID] => 000000000
            [group] => demoCAC
            [currency] => USD
            [enable] => 1
            [read_only] => 0
            [name] => Bogus Demo
            [country] => ****
            [city] => 
            [address] => 
            [phone] => **
            [email] => ***@gmail.com
            [status] => test
            [regdate] => 07/02/2017 09:57
            [lastdate] => 07/02/2017 09:57
            [balance] => 0.00
            [credit] => 0.00
            [equity] => 10000.00
            [isDemo] => 1
            [tp_version] => **
            [last_name] => Demo
            [first_name] => Bogus
            [domain] => ***.com
            [compliance] => 
            [latestTrades] => Array
                (
                    [results] => Array
                        (
                        )

                    [totalResults] => 0
                )

            [owner] => 
        )
    )

So how can i convert it to an array or useable variable ? * This information printed by ( echo ), and i can't access $result -> data ->... is there a function that convert this string to array or json or something readable so i can reach to specific value? when i try : print_r(get_object_vars($result)); i get this error:

Warning: get_object_vars() expects parameter 1 to be object, string given in /home/**/public_html/crm/profile.php on line 20

6
  • 4
    How did you print that information? If you were using var_dump or print_r then your result isn't actually a string but already an object, and thus have access to its properties (eg. $result->success will be equal to the success value, 1 in this case) Commented Feb 8, 2017 at 13:11
  • it seems to be a var_dump display .. you can check eval function php.net/manual/fr/function.eval.php to convert string to php code Commented Feb 8, 2017 at 13:12
  • 1
    Is this the string that your api returns? If it is, you should switch to a format that is easy to parse like json. Commented Feb 8, 2017 at 13:12
  • provide us more code Commented Feb 8, 2017 at 14:11
  • 1
    Also DO NOT USE EVAL! Fky's suggestion is profoundly dangerous and will only lead to a world of hurt. Commented Feb 8, 2017 at 14:19

1 Answer 1

0

Unless Im missing something, your problem is a simple one.

looking at the guts of your var_dump, you should be able to access the index in your question like:

$result->data['email'];  // where 'email' is the key of the data.

or

$result->data['latestTrades']['results']; // nested example.

all the properties of a standard class are public, and so can be accessed directly. And in your example, all of them contain an array, or nested array so can be accessed by their key.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.