How to access the property/ value of an array which has been converted into an object?

我是研究僧i 提交于 2019-12-22 07:40:44

问题


How can I access the property/ value of an array which has been converted into an object? For instance, I want to access the value in the index 0,

$obj = (object) array('qualitypoint', 'technologies', 'India');
var_dump($obj->0);

error,

Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in C:...converting_to_object.php on line 11


回答1:


The reason you can not access values via $obj->0 its because it against PHP variable naming see http://php.net/manual/en/language.variables.basics.php for more information. even if you use ArrayObject you would still have the same issues

but there is a patch to this ... you can convert all integer keys to string or write your own conversion function

Example

$array  = array('qualitypoint', 'technologies', 'India' , array("hello","world"));
$obj = (object) $array;
$obj2 = arrayObject($array);
function arrayObject($array)
{
    $object = new stdClass();
    foreach($array as $key => $value)
    {
        $key = (string) $key ;
        $object->$key = is_array($value) ? arrayObject($value) : $value ;
    }
    return $object ;
}
var_dump($obj2->{0}); // Sample Output
var_dump($obj,$obj2); // Full Output to see the difference 


$sumObject = $obj2->{3} ; /// Get Sub Object
var_dump($sumObject->{1});  // Output world

Output

   string 'qualitypoint' (length=12)

Full output

object(stdClass)[1]
  string 'qualitypoint' (length=12)
  string 'technologies' (length=12)
  string 'India' (length=5)

    array
      0 => string 'hello' (length=5)
      1 => string 'world' (length=5)

object(stdClass)[2]
  public '0' => string 'qualitypoint' (length=12)
  public '1' => string 'technologies' (length=12)
  public '2' => string 'India' (length=5)
  public '3' => 
    object(stdClass)[3]
      public '0' => string 'hello' (length=5)
      public '1' => string 'world' (length=5)

Multi Array Outpur

Thanks

:)




回答2:


Trying this:

$obj = (object) array('test' => 'qualitypoint', 'technologies', 'India');

var_dump($obj->test);

The result is:

string(12) "qualitypoint"

But trying to access $obj->0, the same error shows up: Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'

If you loop through the object, tough, you can access the properties normally as an usual array:

foreach($obj as $x) {
    var_dump($x);
}

Apperantly, the property naming rules are the same as the basic variable naming rules.

If you convert it to an ArrayObject instead, you can access the index normally:

$obj = new ArrayObject(array('qualitypoint', 'technologies', 'India'));

And dumping it:

var_dump($obj[0]);

You would get:

string(12) "qualitypoint"


来源:https://stackoverflow.com/questions/10013143/how-to-access-the-property-value-of-an-array-which-has-been-converted-into-an-o

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