📌  相关文章
📜  如何在PHP使用 stdClass() 将数组转换为对象?

📅  最后修改于: 2022-05-13 01:54:11.472000             🧑  作者: Mango

如何在PHP使用 stdClass() 将数组转换为对象?

要将数组转换为对象,请使用stdClass() 。 stdClass() 是一个空类,用于将其他类型转换为对象。如果将对象转换为对象,则不会对其进行修改。但是,如果对象类型被转换/类型转换,则会创建 stdClass 的实例,如果它不是 NULL。如果为 NULL,则新实例将为空。

示例 1:它使用 stdClass 将数组转换为对象。

PHP
 $value) {
        if (is_array($value)) {
            $value = ToObject($value);
        }
        $object->$key = $value;
    }
    return $object;
}
 
// Declare an array and initialize it
$Original = array (
    '1' => array(
        'sNo' => '1',
        'Age' => '20',
        'name' => 'A'
    ),
    '2' => array(
        'sNo' => '2',
        'Age' => '21',
        'name' => 'B'
    ),
    '3' => array(
        'sNo' => '3',
        'Age' => '22',
        'name' => 'C'
    ),
    '4' => array(
        'sNo' => '4',
        'Age' => '23',
        'name' => 'D'
    ),
    '5' => array(
        'sNo' => '5',
        'Age' => '24',
        'name' => 'E'
    )
);
 
// Display the original array
print_r($Original);
 
// Function call
$convertedObj = ToObject($Original);
 
// Display the stdClass object
print_r($convertedObj);
 
?>


PHP
 $value) {
        if (is_array($value)) {
            $value = ToObject($value);
        }
        $object->$key = $value;
    }
    return $object;
}
 
// Declare an array
$Original = array(1, 2, 3, 4, 5, 6);
 
// Display the array element
print_r($Original);
 
// Function call to convert object
$convertedObj = ToObject($Original);
 
// Display the stdClass object
print_r($convertedObj);
 
?>


输出:
Array
(
    [1] => Array
        (
            [sNo] => 1
            [Age] => 20
            [name] => A
        )

    [2] => Array
        (
            [sNo] => 2
            [Age] => 21
            [name] => B
        )

    [3] => Array
        (
            [sNo] => 3
            [Age] => 22
            [name] => C
        )

    [4] => Array
        (
            [sNo] => 4
            [Age] => 23
            [name] => D
        )

    [5] => Array
        (
            [sNo] => 5
            [Age] => 24
            [name] => E
        )

)
stdClass Object
(
    [1] => stdClass Object
        (
            [sNo] => 1
            [Age] => 20
            [name] => A
        )

    [2] => stdClass Object
        (
            [sNo] => 2
            [Age] => 21
            [name] => B
        )

    [3] => stdClass Object
        (
            [sNo] => 3
            [Age] => 22
            [name] => C
        )

    [4] => stdClass Object
        (
            [sNo] => 4
            [Age] => 23
            [name] => D
        )

    [5] => stdClass Object
        (
            [sNo] => 5
            [Age] => 24
            [name] => E
        )

)

示例 2:它使用 stdClass 将数组转换为对象。

PHP

 $value) {
        if (is_array($value)) {
            $value = ToObject($value);
        }
        $object->$key = $value;
    }
    return $object;
}
 
// Declare an array
$Original = array(1, 2, 3, 4, 5, 6);
 
// Display the array element
print_r($Original);
 
// Function call to convert object
$convertedObj = ToObject($Original);
 
// Display the stdClass object
print_r($convertedObj);
 
?>
输出:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
stdClass Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)