0

I need to be able to create an object from an array. The data is coming from a terrible API. Here is an example of the data.

Array
(
[0] => stdClass Object
    (
        [no] => 1
        [FL] => Array
            (
                [0] => stdClass Object
                    (
                        [content] => 1505250000001141005
                        [val] => ACCOUNTID
                    )

                [1] => stdClass Object
                    (
                        [content] => 1505250000000091001
                        [val] => SMOWNERID
                    )

                [2] => stdClass Object
                    (
                        [content] => name here
                        [val] => Account Owner
                    )
ETC...

The [no] value is insignificant. the [FL] content is everything I need in the object. So the outcome I would like is all of the values will be variable names and content will be the values. Can anyone suggestion anything to help please?

EDIT I think I need to clarify what I need. I'm creating a class called Account which will hold all the information from the api call in an object. I will instantiate the object then call a method for example valuesFromArray() and pass it the [FL] array given by the api call. it will use [val] as the variable name and [content] as its value.

1 Answers1

0
     $json_url = "API URL";
    //  Initiate curl
    $ch_curl = curl_init();
    // Disable SSL verification
    curl_setopt($ch_curl, CURLOPT_SSL_VERIFYPEER, false);
    // Will return the response, if false it print the response
    curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);
    // Set the url
    curl_setopt($ch_curl, CURLOPT_URL, $json_url);
    // Execute
    $result = curl_exec($ch_curl);
    // Closing
    curl_close($ch_curl);
    $data = json_decode($result, true);//true--> Convert object to array format

This is the curl method to fetch data from API.Use json_decode($result, true) use json decode convert object to array format.

I hope this is usefull for your question.

Please review,

Thanks!

Karthik Keyan
  • 424
  • 4
  • 15