php数组转换为对象PHP - Convert Array to Object with stdClass
2011-10-20 21:47
761 查看
原文:http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass
The PHP stdClass() is something that isn't well documented but i will try to shed some light into the matter. stdClass is a default PHP object which has no predefined members. The name stdClass is used internally by Zend and is reserved. So that means that you cannot define a class named stdClass in your PHP code.
It can be used to manually instantiate generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.
Here's an example below that converts an array to an object below. This method is called Type Casting.
The PHP stdClass() is something that isn't well documented but i will try to shed some light into the matter. stdClass is a default PHP object which has no predefined members. The name stdClass is used internally by Zend and is reserved. So that means that you cannot define a class named stdClass in your PHP code.
It can be used to manually instantiate generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.
Here's an example below that converts an array to an object below. This method is called Type Casting.
相关文章推荐
- php数组转换为对象PHP - Convert Array to Object with stdClass
- 使用json_decode()转换返回数组或对象时出现:Cannot use object of type stdClass as array
- PHP简单 对象(object) 与 数组(array) 的转换
- 把排序数组转换为高度最小的二叉搜索树convert-sorted-array-to-binary-search-tree-with-minimal-height
- PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)
- php--------对象(object) 与 数组(array) 的转换
- PHP Object对象转换为Array数组
- php--------对象(object) 与 数组(array) 的转换
- php--------对象(object) 与 数组(array) 的转换
- php中对象(object)与数组(array)之间的相互转换
- lintcode convert-sorted-array-to-binary-search-tree-with-minimal-height 把排序数组转换为高度最小的二叉搜索树
- PHP简单 对象(object) 与 数组(array) 的转换
- php 对象转数组 objectToArray
- Json convert to object with gjson.(Json 转换为类对象)
- **PHP错误Cannot use object of type stdClass as array in错误的
- PHP------数组和对象相互转化,stdClass Object转array
- 8.5 Replace Array with Object(以对象取代数组)
- Replace Array with Object (以对象取代数组)
- LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)
- 9. 11. 30. 从asList到toArray转换 -Convert a List (ArrayList) to an Array with zero length array