php object attribute with dot in name

大憨熊 提交于 2019-12-17 07:47:29

问题


I have mysql table with collumns like 'operation.date', 'operation.name' and etc. After fetching that table data as object with $mysqli->fetch_object() i get this (print_r of row):

stdClass Object
(
[id] => 2
[operation.date] => 2010-12-15
[operation.name] => some_name
)

how do I acces operation.date and operation.name and all other weirdly named object properties?


回答1:


Specify aliases in your SQL query like SELECT column AS nameWithoutDots ...
or access these properties with $object->{'operation.name'}
or cast the object to array like this: $obj = (array)$obj; echo $obj['operation.name'].




回答2:


The correct way of accessing properties with a dot should be :

echo $object->{"operation.date"}



回答3:


To access these attributes you need to wrap them with curly brackets:

echo $object->{"operation.date"} //2010-12-15

If you set an attribute this way the offending symbol gets removed, allowing you to access the attribute as echo $object->operationdate //2010-12-15




回答4:


Change the sql to return valid property names using the 'as' feature

eg. select operation.date as date




回答5:


You can get assoc array instead object by using $mysqli->fetch_assoc()



来源:https://stackoverflow.com/questions/5351018/php-object-attribute-with-dot-in-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!