利用ObjMap将多维数组转换成Object
相必大家都知道 stdClass 类, 但是对于多维数组呢? 我们不能就这样的转换,
这这可看成是PHP5的一个基类, 提供了类似于数组的调用方法
可以通过显式的方法将一个数组转换成stdClass,然后通过用对像的方式访问
复制PHP内容到剪贴板
PHP代码:
$a = new stdClass();
$a->b = 1;
echp $a->b; // output:1
// arr->obj
$arr = array(a,b);
$obj = (object)$arr;
为什么不用数组呢? 对于PHP来说用数组不是更方便吗?
1. 我喜欢用对像的调用方式,写起来方便,顺畅
2. 数组是COPY值,对像是能过引用的
3. 可以实现一些特殊的功能,(全局静态变量,这个以后再说)
下面这个类.将会实现这样的方法,
至于可以用在什么地方.大家可以发挥
复制PHP内容到剪贴板
PHP代码:
$data = array(a1=>array(b1=>b1value,b2=>b2value,b3=>b3value));
$data = new map($data);
// OBJ 取值
echo $data->a1->b1; // output: b1value
// OBJ 赋值
$data->a1->b2 = newb2value;
echo
.$data->a1->b2; //output: newb2value
// ARRAY 取值
echo
.$data[a1][b3]; //output: b3value
// FOREACH 循环
// output: b1=>b1value b2=>newb2value b3=>b3value
foreach($data->a1 as $key=>$val){
echo
.$key.=>.$val;
}
class map
复制PHP内容到剪贴板
PHP代码:
class map extends ArrayObject{
// 获取 arrayobject 因子
public function __construct(array $array = array()){
foreach ($array as &$value){
is_array($value) && $value = new self($value);
}
parent::__construct($array);
}
// 取值
public function __get($index){
return $this->offsetGet($index);
}
// 赋值
public function __set($index, $value){
is_array($value) && $value = new self($value);
$this->offsetSet($index, $value);
}
// 是否存在
public function __isset($index){
return $this->offsetExists($index);
}
// 删除
public function __unset($index){
$this->offsetUnset($index);
}
// 转换为数组类型
public function toArray(){
$array = $this->getArrayCopy();
foreach ($array as &$value){
($value instanceof self) && $value = $value->toArray();
}
return $array;
}
// 打印成字符
public function __toString(){
return var_export($this->toArray(), true);
}
// 根据索引赋值
public function put($index,$value){
is_array($value) && $value = new self($value);
$this->offsetSet($index, $value);
}
// 根据索引取值
public function get($index){
return $this->offsetGet($index);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

As a world-renowned short video social platform, Douyin has won the favor of a large number of users with its unique personalized recommendation algorithm. This article will delve into the value and principles of Douyin video recommendation to help readers better understand and make full use of this feature. 1. What is Douyin recommended video? Douyin recommended video uses intelligent recommendation algorithms to filter and push personalized video content to users based on their interests and behavioral habits. The Douyin platform analyzes users' viewing history, like and comment behavior, sharing records and other data to select and recommend videos that best suit users' tastes from a huge video library. This personalized recommendation system not only improves user experience, but also helps users discover more video content that matches their preferences, thereby enhancing user stickiness and retention rate. at this

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.
