尊龙凯时人生就是搏

ThinkPHP5µÄÊý×éת¹¤¾ß¼¼ÇÉÓÐÄÄЩ

Ò»¡¢Êý×éת¹¤¾ßÏÈÈÝ

ÔÚPHP¿ª·¢ÖУ¬ÎÒÃǾ­³£ÐèÒª½«Êý×éת»»³É¹¤¾ßÒÔ±ã¸üÀû±ãµØʹÓã¬ÄÇôÔõÑùʵÏÖÊý×éÓ빤¾ßµÄת»»ÄØ£¿

ʹÓà PHP ÖÐµÄ stdClass() À࣬ThinkPHP5 ¿ÉÒÔÌṩÀû±ã¿ì½ÝµÄÊý×éת»»Îª¹¤¾ßµÄÒªÁ졣ʹÓÃPHPÄÚÖõĸÃÀà¿ÉÒÔ¶¯Ì¬½¨É蹤¾ß£¬²¢½«Êý×éת»»³É¹¤¾ß£¬²Ù×÷ÇáÓ¯¡£

¶þ¡¢Ê¹ÓÃstdClass()ʵÏÖÊý×éת¹¤¾ß

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±  £»

ÏÂÃæÊÇʹÓÃThinkPHP5ÖеÄstdClass()ÀàʵÏÖÊý×éת¹¤¾ßµÄʾÀý´úÂ룺

$array = array('name' => 'ThinkPHP', 'url' => 'www.thinkyisu.com');

$obj = (object)$array;

echo $obj->name; // Êä³ö£ºThinkPHP
echo $obj->url; // Êä³ö£ºwww.thinkyisu.com

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏȽç˵ÁËÒ»¸öÊý×é$array£¬°üÀ¨nameºÍurlÁ½¸öÔªËØ¡£È»ºóʹÓÃ(object)Ç¿ÖÆÀàÐÍת»»½«¸ÃÊý×éת»»³É¹¤¾ß$obj£¬²¢Í¨¹ý$obj->nameºÍ$obj->url»á¼û¸Ã¹¤¾ßÊôÐÔµÄÖµ¡£

Èý¡¢Ê¹ÓÃÊý×éת»»¹¤¾ßʵÏÖÊý×éת¹¤¾ß

ÎÒÃDz»µ«¿ÉÒÔʹÓÃÄÚÖõÄstdClass()À࣬»¹¿ÉÒÔʹÓõÚÈý·½µÄÊý×éת»»¹¤¾ßÀ´°ÑÊý×éת»»³É¹¤¾ß¡£ÕâЩ¹¤¾ß²»µ«ÊÊÓÃÓÚ½«Êý×éת»»³É¹¤¾ß£¬»¹Ö§³Ö¹¤¾ßºÍÊý×éÖ®¼äµÄÏ໥ת»»¡£³£¼ûµÄPHPÊý×éת»»¹¤¾ßÓÐJsonSerializable¡¢HydratorºÍArraySerializableµÈ¡£

ÏÂÃæÎÒÃÇÒÔJsonSerializableΪÀý£¬¼òÒªÏÈÈÝ×ÅʵÏÖÊý×éת¹¤¾ßµÄÒªÁ죺

class User implements JsonSerializable
{
    private $id;
    private $name;
    private $email;

    public function __construct($id, $name, $email)
    {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }

    public function jsonSerialize()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email
        ];
    }
}

$userArray = array('id' => 1, 'name' => 'Tom', 'email' => 'tom@test.com');

$user = new User($userArray);

$json = json_encode($user);

echo $json;

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÌåÏÖÓû§ÐÅÏ¢µÄUserÀ࣬²¢ÊµÏÖÁËJsonSerializable½Ó¿Ú£¬¸Ã½Ó¿ÚÖнç˵ÁËjsonSerialize()ÒªÁìÓÃÓÚÐòÁл¯ÐèÒª±»JSON±àÂëµÄÊý¾Ý¡£´Ë´¦ÎÒÃǽ«Óû§µÄid¡¢nameºÍemailÊôÐÔÐòÁл¯³ÉÒ»¸öÊý×é¡£½ÓÏÂÀ´£¬ÎÒÃǽç˵һ¸öÓû§ÐÅÏ¢Êý×é$userArray£¬Ê¹ÓøÃÊý×éÌìÉúÓû§¹¤¾ß$user£¬È»ºóʹÓÃjson_encode()ÒªÁ콫¸Ã¹¤¾ß±àÂëΪJSONÃûÌ㬲¢Êä³öJSON×Ö·û´®¡£

ÒÔÉϾÍÊÇThinkPHP5µÄÊý×éת¹¤¾ß¼¼ÇÉÓÐÄÄЩµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ