<?php $array = array('asd'=>"Hello",'World'); $obj = (object)$array; 

How can I refer to the object to bring the World ?

$obj->1 is this wrong?

    2 answers 2

    Arrays are converted to object with field names named according to the array keys and their corresponding values, with the exception of numeric keys that will not be available until the object is not iterated .

    A source

     <?php $array = array('asd'=>"Hello", 'World'); $obj = (object)$array; foreach($obj as $key => $value) { print "$key => $value\n"; } 

    Result

     asd => Hello 0 => World 
    • thank you so much - vov4ok
    • It is curious that $arr = ["\0" => ':-)']; $obj = (object) $arr; $arr = ["\0" => ':-)']; $obj = (object) $arr; it will not be possible to iterate, but you can lead to an array and iterate it. - Yegor Banin

    This is not an object, but an array. You can call it like this: $ array [0]

    Because for php your record will be like:

     array:2 [ "asd" => "Hello", 0 => "World"] 
    • I disagree with you, $ obj is an object. - Visman
    • I after all can turn so echo $ obj-> asd; // Hello; echo $ obj-> 0; // FATAL ERROR - vov4ok
    • and I understand that I can turn to the array, but how can I turn to the properties of the converted object, is it possible? - vov4ok
    • To access an object, you must first create this object. The basics of OOP in php can be found here: php.net/manual/ru/oop5.intro.php , in this context there is an array php.net/manual/ru/language.types.array.php , and I advise you to work with it with an array - Lyrium
    • @Lyrium if you look closely, then $ obj і is an object і if you create print_r ($ obj) then you are obsessed with // stdClass Object ([asd] => Hello [0] => World) - vov4ok