Modelo

  • EN
    • English
    • Español
    • Français
    • Bahasa Indonesia
    • Italiano
    • 日本語
    • 한국어
    • Português
    • ภาษาไทย
    • Pусский
    • Tiếng Việt
    • 中文 (简体)
    • 中文 (繁體)

How to Convert Std Class Object to Array

Sep 29, 2024

Do you want to convert a std class object to an array in PHP? Well, you're in luck! Here's a simple way to do it using the built-in json_decode and json_encode functions.

Step 1: Use json_encode to convert the std class object to a JSON string. This function takes an object as a parameter and returns a JSON-formatted string.

Step 2: Use json_decode to convert the JSON string back to an associative array. This function takes the JSON string and an optional boolean parameter to indicate whether the result should be converted to an associative array.

Here's a code example to illustrate the process:

```php

// Sample std class object

$stdObject = new stdClass();

$stdObject->name = 'John Doe';

$stdObject->age = 25;

// Convert std class object to array

$jsonString = json_encode($stdObject);

$array = json_decode($jsonString, true);

// Print the converted array

print_r($array);

?>

```

And that's it! You've successfully converted a std class object to an array using the json_decode and json_encode functions. This method is simple, quick, and efficient, making it a great choice for your PHP projects. Give it a try and see the results for yourself!

Recommend