As @rray said in your comment
You can even set numerical properties on an object but you can not access them. That's the problem.
Using the get_object_vars
feature is clear:
var_dump(get_object_vars($obj));
array(0) {
}
In other words, it did not generate any attributes in the class, but only doing:
var_dump($obj);
object(stdClass)#48 (2) {
[0]=>
array(2) {
["nome"]=>
string(12) "nome exemplo"
["idade"]=>
string(13) "idade exemplo"
}
[1]=>
array(2) {
["nome"]=>
string(14) "nome exemplo 2"
["idade"]=>
string(15) "idade exemplo 2"
}
}
Possible solution
$array[0]["nome"] = "nome exemplo";
$array[0]["idade"] = "idade exemplo";
$array[1]["nome"] = "nome exemplo 2";
$array[1]["idade"] = "idade exemplo 2";
foreach ($array as $k => $attributes){
foreach ($attributes as $attribute => $value) {
$array[$attribute][$k] = $value;
unset($array[$k]);
}
}
$obj = (object) $array;
So you're reversing the order and generating an associative array. Which in the conversion generates attributes in the class.
var_dump(get_object_vars($obj));
array(2) {
["nome"]=>
array(2) {
[0]=>
string(12) "nome exemplo"
[1]=>
string(14) "nome exemplo 2"
}
["idade"]=>
array(2) {
[0]=>
string(13) "idade exemplo"
[1]=>
string(15) "idade exemplo 2"
}
}
However, as you can see, it does not make much sense to do this because, since you have multiple name values, you will still have an array.