error in getting values from stdClass php array

Asked

Viewed 1,514 times

3

When I use echo var_dump($location); I get this:

object(stdClass)#1226 (2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(44.0928438) [1]=> float(-70.20876942) } }

I tried to obtain the numbers floats((44.0928438, -70.20876942)), with the following code:

$lat = $location["type"]["coordinates"][0];
$long = $location["type"]["coordinates"][1];

But when I run my php file, it gives me the following error not because:

Fatal error: Cannot use Object of type stdClass as array in /Applications/XAMPP/xamppfiles/htdocs/aw014/Externalwebserviceadapter/Adapters/Twitteradapter.class.php on line 62

  • In addition to the more elaborate responses, a var_dump() followed by a echo is not only wrong but unnecessary, since var_dump() already sends output to the browser. ^_^

  • I think the var_dump was just to show the content of the object as a form of debugging, should not be to display given to the browser...

  • Put the $location in your code ! if you could see? Because I wonder if a class has to use -> !!! but first I need to see $location

  • @Wakim, yes, yes, but am I cranky, do what? p

  • @Brunoaugusto, understood, no problem hehe

1 answer

2


It turns out that the square bracket syntax until some time ago was an exclusivity of the arrays.

However, little by little (relatively speaking) the Objects were being improved. First they could be iterated as a normal array. The natural consequence of this would be that objects representing an array could also adopt the square bracket syntax.

This ability is to make an object implement the interface Arrayaccess (and its methods). Ironically, it seems that the class stdClass did not receive this implementation and, therefore, the Fatal Error.

To solve the problem you have two options:

  1. Map this stdClass Object to an array:

    function map( $param ) {
    
        if( is_object( $param ) ) {
            $param = get_object_vars( $param );
        }
    
        if( is_array( $param ) ) {
            return array_map( __FUNCTION__, $param );
        }
    
        return $param;
    }
    

    It is also possible to force the cast from that object to array:

    $data = (array) $original;
    

    But this is not recursive. In your case it would not be a problem because you have a single object stdClass, but the above function still holds as reference as it converts to array as many stdClass how many she meets, hierarchically speaking.

  2. Find alternatives to the result not come in a stdClass

    By the content of the entries of this object it is remarkable that there is the consumption of a Geotargeting API or at least one fetching database data using objects such as fetching style

    In the case of an API, usually the data is traffic in JSON which is a "universal" format. To work with JSON you use PHP json_decode() with the intent to produce an array.

    For PHP, your intentions are almost always worthless. That’s why, by default, json_decode() returns data in a stdClass.

    However, it is simple to get around this. Just pass the second argument json_decode() as TRUE:

    $data = jsondecode( $original, TRUE );
    

    If the data are under the influence of a fetching style check the documentation of how you read the query feature by setting that no objects are used.

    In the PDO, for example, by default, a resource is returned in both indexed and associative arrays (PDO::FETCH_BOTH), but it is quite common to be changed to PDO::FETCH_ASSOC or, in case it might be your, PDO::FETCH_OBJ

  • Thank you solved my question

  • But what scenario did I hit? JSON or database?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.