Wednesday 15 September 2010

php - Add object to array with given key -


I have a class with a private array. When I import a file, I want to add this new object to the object (which has been created) to add it. I have a name in the object that I want to use as a key, so:

  & lt ;? Php $ object = new object (); $ Object-> Name = "test"; Class Other Object {Private $ loaded_files = array (); Public function addObject ($ fileObj) {echo number ($ this-> loaded_file); // results 0 $ this-> Loaded_file [$ fileObj- & gt; Name] = clone $ fileObj; Resonance count ($-- loaded_files); // result0 // array_push ($ this-> loaded_files, clone $ fileObj); // $ this- & gt; Loaded_file ["hard-coded"] = clone $ fileObj; }} $ Other Object = New Other Object (); $ OtherObject- & gt; AddObject ($ item); ? & Gt;   

As you can see in my example, I want to use the name as a key and want to clone the object so that it can be placed in the array. After the check array_push () (hence, no key) and hard-coded key works fine, but $ fileObj- & gt; Name does not Does anyone know how this is possible?

Edit: The example code changed slightly, this is not my production code. I hope I have one but keep it zero. Only when I do not use a key or hard coded key, the object is attached.

OK I found trouble. @Bermar, AlexSherOv, Thanks for helping.

The problem was that it was not trimmed before it was added to the object. After adding the trim ($ value) all the elements have solved the problem and the element has been added to the array :)

No comments:

Post a Comment