किसी ऑब्जेक्ट को PHP में साहचर्य सरणी में बदलने के लिए, कोड इस प्रकार है-
उदाहरण
<?php class department { public function __construct($deptname, $deptzone) { $this->deptname = $deptname; $this->deptzone = $deptzone; } } $myObj = new department("Marketing", "South"); echo "Before conversion:"."\n"; var_dump($myObj); $myArray = json_decode(json_encode($myObj), true); echo "After conversion:"."\n"; var_dump($myArray); ?>
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा-
Before conversion: object(department)#1 (2) { ["deptname"]=> string(9) "Marketing" ["deptzone"]=> string(5) "South" } After conversion: array(2) { ["deptname"]=> string(9) "Marketing" ["deptzone"]=> string(5) "South" }
उदाहरण
आइए अब एक और उदाहरण देखें -
<?php class department { public function __construct($deptname, $deptzone) { $this->deptname = $deptname; $this->deptzone = $deptzone; } } $myObj = new department("Marketing", "South"); echo "Before conversion:"."\n"; var_dump($myObj); $arr = (array)$myObj; echo "After conversion:"."\n"; var_dump($arr); ?>
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा-
Before conversion: object(department)#1 (2) { ["deptname"]=> string(9) "Marketing" ["deptzone"]=> string(5) "South" } After conversion: array(2) { ["deptname"]=> string(9) "Marketing" ["deptzone"]=> string(5) "South" }