Created
February 20, 2016 12:14
-
-
Save Balamir/cfacfd5fc1b617c38c71 to your computer and use it in GitHub Desktop.
PHP stdClass Object to Array and Array to stdClass Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Array veriyi Object'e donusturur | |
| * | |
| * Belirtilen array veriyi, tum alt array'lerle birlikte | |
| * object'e donusturerek cikti verir | |
| * | |
| * @since 2.0 | |
| * | |
| * @param array $array_data Object'e donusturulecek array veri | |
| * @return object Object'e donusturulmus veri | |
| */ | |
| function sd_object_to_array( $array_data ) { | |
| if ( is_object( $array_data ) ) { | |
| // get_object_vars() fonksiyonu ile | |
| // belirtilen objenin ozelliklerini al | |
| $array_data = get_object_vars( $array_data ); | |
| } | |
| if ( is_array( $array_data ) ) { | |
| /* | |
| * Objeye donusturulen array'i dondur | |
| * Tum alt oge (recursive) cagrilari icin | |
| * __FUNCTION__ (Buyulu sabit) kullan | |
| */ | |
| return array_map( __FUNCTION__, $array_data ); | |
| } | |
| else { | |
| // Array'i dondur | |
| return $array_data; | |
| } | |
| } | |
| /** | |
| * Object veriyi Array'e donusturur | |
| * | |
| * Belirtilen object veriyi, tum alt object'lerle birlikte | |
| * array'e donusturerek cikti verir | |
| * | |
| * @since 2.0 | |
| * | |
| * @param object $object_data Array'e donusturulecek object veri | |
| * @return array Array'e donusturulmus veri | |
| */ | |
| function sd_array_to_object( $object_data ) { | |
| if ( is_array( $object_data ) ) { | |
| /* | |
| * Array'a donusturulen object'i dondur | |
| * Tum alt oge (recursive) cagrilari icin | |
| * __FUNCTION__ (Buyulu sabit) kullan | |
| */ | |
| return (object) array_map( __FUNCTION__, $object_data ); | |
| } | |
| else { | |
| // Object'i dondur | |
| return $object_data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment