本文介绍了如何将数组转换成使用stdClass的一个对象()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了以下数组:
$clasa = array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
我想知道如何将此数组转换成使用stdClass的(),我是一个PHP初学者的对象,一个简单的例子是非常有益的,我试着寻找类似的问题,但答案是复杂的并且超越我的基本类和对象的理解。
I would like to know how to convert this array into an object using stdClass(), I'm a PHP beginner, a simple example would be very helpful, I've tried searching for similar questions, but the answers are complicated and go beyond my understanding of basic classes and objects.
推荐答案
您只需添加此code
You just add this code
$clasa = (object) array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
如果您希望看到的是这个stdClass的对象只是调用这个
If you want to see is this stdClass object just call this
print_r($clasa);
如果要转换一个数组对象code将
If you want to convert an array to object code will be
$arr = array('a'=>'apple','b'=>'ball');
$arr = (object) $arr;
您不需要使用stdClass的。它会自动转换为stdClass的
You don't need to use stdClass. It will automatically converted to stdClass
这篇关于如何将数组转换成使用stdClass的一个对象()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!