How to convert an array to object in PHP?

前端 未结 30 2837
说谎
说谎 2020-11-22 02:48

How can I convert an array like this to an object?

[128] => Array
    (
        [status] => "Figure A.
 Facebook\'s horizontal scrollbars showing u         


        
30条回答
  •  死守一世寂寞
    2020-11-22 03:23

    Multidimensional arrays into an object. this code is used for conversion of Bing search API try and catch method.

    try {
            // Perform the Web request and get the JSON response
            $context = stream_context_create($options);
            $results = file_get_contents($url . "?cc=" . $country . "&category=" . $type, false, $context);
            $results = json_decode($results);
            return response()->json($results);
        } catch (\Exception $e) {
            $results = array('value' => array(
                    (object) array(
                        "name" => "Unable to Retrive News",
                        "url" => "http://www.sample.com/",
                        "image" => (object) array("thumbnail" => (object) array("contentUrl" => "")),
                        "publishedAt" => "",
                        "description" => "")
                )
            );
            $results = (object) $results;
            return response()->json($results);
        }
    

提交回复
热议问题