当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP stdClass()用法及代码示例


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

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

PHP


<?php
// Function to convert array into
// stdClass object
function ToObject($Array) {
     
    // Create new stdClass object
    $object = new stdClass();
     
    // Use loop to convert array into
    // stdClass object
    foreach ($Array as $key => $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);
?>
输出:
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


<?php
// Function to convert array into
// stdClass object
function ToObject($Array) {
     
    // Create new stdClass object
    $object = new stdClass();
     
    // Use loop to convert array into object
    foreach ($Array as $key => $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
)



相关用法


注:本文由纯净天空筛选整理自PranchalKatiyar大神的英文原创作品 How to convert an array into object using stdClass() in PHP?。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。