Using ObjMap to convert a multi-dimensional array to an Object, you must know the stdClass class when using ObjMap to convert a multi-dimensional array to an Object. this can be seen as a base class of PHP5, provides an array-like call method to convert an array to stdClass through an explicit method, and then access and copy the PHP content to the clipboard in the way of an image. PHP "> <LINKhr
Everyone must know the stdClass class,
This can be seen as a base class of PHP5, providing a call method similar to an array
You can use an explicit method to convert an array to stdClass, and then access
Copy the PHP content to the clipboard.
PHP code:
$ A = new stdClass ();
$ A-> B = 1;
Echp $ a-> B; // output: 1
// Arr-> obj
$ Arr = array ('A', 'B ');
$ Obj = (object) $ arr;
Why not use arrays? Isn't array more convenient for PHP?
1. I like to use the method of calling the image, which is easy to write and smooth.
2. the array is the COPY value, and the object can be referenced.
3. some special functions can be implemented (Global static variables will be added later)
But what about multi-dimensional arrays? We cannot convert it like this,
The following class will implement this method,
Where can we use it?
Copy the PHP content to the clipboard.
PHP code:
$ Data = array ('A1' => array ('b1 '=> 'b1value', 'B2' => 'b2value ', 'b3' => 'b3value '));
$ Data = new map ($ data );
// OBJ value
Echo $ data-> a1-> b1; // output: b1value
// OBJ value assignment
$ Data-> a1-> b2 = 'newb2value ';
Echo'
'. $ Data-> a1-> b2; // output: newb2value
// ARRAY Value
Echo'
'. $ Data ['A1'] ['b3']; // output: b3value
// FOREACH loop
// Output: b1 => b1value b2 => newb2value b3 => b3value
Foreach ($ data-> a1 as $ key => $ val ){
Echo'
'. $ Key.' => '. $ val;
}
Class map
Copy the PHP content to the clipboard.
PHP code:
Class map extends ArrayObject {
// Obtain the arrayobject factor
Public function _ construct (array $ array = array ()){
Foreach ($ array as & $ value ){
Is_array ($ value) & $ value = new self ($ value );
}
Parent: :__ construct ($ array );
}
// Value
Public function _ get ($ index ){
Return $ this-> offsetGet ($ index );
}
// Assign a value
Public function _ set ($ index, $ value ){
Is_array ($ value) & $ value = new self ($ value );
$ This-> offsetSet ($ index, $ value );
}
// Existence
Public function _ isset ($ index ){
Return $ this-> offsetExists ($ index );
}
// Delete
Public function _ unset ($ index ){
$ This-> offsetUnset ($ index );
}
// Convert to array type
Public function toArray (){
$ Array = $ this-> getArrayCopy ();
Foreach ($ array as & $ value ){
($ Value instanceof self) & amp; $ value = $ value-> toArray ();
}
Return $ array;
}
// Print it into characters
Public function _ toString (){
Return var_export ($ this-> toArray (), true );
}
// Assign values based on the index
Public function put ($ index, $ value ){
Is_array ($ value) & $ value = new self ($ value );
$ This-> offsetSet ($ index, $ value );
}
// Based on the index value
Public function get ($ index ){
Return $ this-> offsetGet ($ index );
}
}