1

I have an array as below -

$xml= array(
                'name'=> 'Arun',
                'roll' => '12345',
                'id' => '1'
        );

I want this to be converted to User Defined Object [Not stdClass Nor SimpleXMLObject]

I want it something like below -

UserDefinedObject Object 
(
    [name] => Arun
    [roll] => 12345
    [id] => 1
)

I have done something like belo, which gives me stdClass Object, but I need this to be UserDefined -

public static function convert($xml)
    {
        //$array = new ConvertArrayToXML($xml);
        //return $array;
        //return((Object)$xml);
        foreach($xml as $key => $value)
        {
            if(is_array($value))
            {
                $array[$key] = self::convert($value);
            }
        }
        return (object)$xml;
    }

I know this may sound very silly, but I am very new to casting user defined objects in php.

Please give some pointers on this.

Thanks.

  • @Arun: Your previous question has been closed for a reason. Please do not make useless duplicate postings on site. There are really many existing Q&A question on how to turn something array into something XML with PHP here on site. Same for the question on how to turn the array into a user-defined class. Please search existing material first. Also you have some tendency to ask slighly off, most likely you'll reverse most of your findings. See as well http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – hakre Apr 28 '15 at 17:34
  • this was for me for example the first hit on google: http://stackoverflow.com/q/1869091/367456 - search was "array to object php" - just saying. – hakre Apr 28 '15 at 17:39
  • @casperone Hi Moderator. Can You please have a look into my questions Which hakre has marked as duplicate and let me know if they were really deserve to be marked as duplicate ? Also I don't like the kind of language hakre uses on StackOverFlow. Its not at all acceptable. Can you guys do something about it ? –  Apr 28 '15 at 18:00
  • @Arun There is nothing wrong with Hakre's language, and his advice is valid, though this specific closure was somewhat inappropriate. – user229044 Apr 28 '15 at 18:18
  • @hakre This was not a duplicate of the question you linked to, please don't abuse close-as-duplicate to unilaterally close questions unless you find an *actual* duplicate. – user229044 Apr 28 '15 at 18:19
  • @meagar: Yes, it was slightly wrong, the (here dead) code triggerd a pattern of this mornings similar quesiton with exactly that code. – hakre Apr 28 '15 at 18:22

3 Answers3

1
$xml = [
    'name'=> 'Arun',
    'roll' => '12345',
    'id' => '1',
];

class UserDefinedObject { }

$myObj = new UserDefinedObject();
foreach($xml as $key => $value) { 
    $myObj->{$key} = $value; 
}

var_dump($myObj);

EDIT

or a slight variant:

$myObj = new UserDefinedObject();
array_walk($xml,
    function($value, $key) use ($myObj) {
        $myObj->{$key} = $value; 
    }
);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • This "class" is just a `stdClass` with a different name. How is it better than using plain `stdClass`? – axiac Apr 28 '15 at 13:59
  • Thanks Mark this is something which I was looking for. –  Apr 28 '15 at 14:02
  • @axiac.... it's not a stdClass at all, it can have whatever methods, etc you want as defined in `class UserDefinedObject { }`.... just because I've chosen not to write several hundred lines of class code to keep this example brief doesn't make it any less of a user-defined class – Mark Baker Apr 28 '15 at 14:07
1
class UserDefinedClass {
public $name;
public $roll;
public $id;

    function __construct($name,$roll,$id){
        $this->name = $name;
        $this->roll = $roll;
        $this->id = $id;
    }
}
$xml= array(
     'name'=> 'Arun',
     'roll' => '12345',
     'id' => '1'
);


$obj = new UserDefinedClass($xml['name'],$xml['roll'],$xml['id']);
// you can use and foreach for dynamic elements.
print_r($obj);
Kristiyan
  • 1,655
  • 14
  • 17
0

$xml = array('name' => 'Arun', 'roll' => '12345', 'id' => '1');

class UserDefined {

public $name;
public $roll;
public $id;

function __construct($xml) {
    $this->name = $xml['name'];
    $this->roll = $xml['roll'];
    $this->id = $xml['id'];
}

}

print_r(new UserDefined($xml));

arushi
  • 404
  • 5
  • 15