tags:

views:

15

answers:

2

Hello everyone. I have an object, and I have a new value which I need to add to this object, but not just to the end of it, that would be no problem accomplishing. What I need to do is add this value to a new node on a specific index.

Ok so here's the object.

[object]=>{ 
  ["a"]=> "value" 
  ["b"]=> "value" 
  ["c"]=> "value"
  ["d"]=> "value"
} 

Then I have this fifth value that I now need to add to index 1, or as it's named in this example "b". Like this.

[object]=>{ 
  ["a"]=> "value"
  ["e"]=> "fifth value"
  ["b"]=> "value" 
  ["c"]=> "value"
  ["d"]=> "value"
} 

So the question remains, is there a smart way to do this or do I have to split them up and make them into arrays and merge them as arrays then assign them to a new object explicitly telling it to be an object?

Like this.

$object = (object)array_merge((array)$first, (array)$second);

I feel that there must be a better way to handle this, with only objects.

Thank you for your time.

+2  A: 

What are you doing that needs this:

[object]=>{ 
  ["a"]=> "value"
  ["e"]=> "fifth value"
  ["b"]=> "value" 
  ["c"]=> "value"
  ["d"]=> "value"
} 

to be different from this:

[object]=>{ 
  ["a"]=> "value"
  ["b"]=> "value" 
  ["c"]=> "value"
  ["d"]=> "value"
  ["e"]=> "fifth value"
} 

?

If you NEED them to be different, you are using OOP wrong.

EDIT: What's stopping you from using arrays?

quantumSoup
Yes I know I can get the values from every index anyways, but I need them to be in a specific order I'm afraid. I'm iterating over objects and printing the nodes in order. The objects aren't always the same, so I'm trying to handle this by sending them from the server in the order I want them. Big inconvenience I might add. :)
Stefan Konno
To answer your edit. I'm sending the object from the server as a json object. So there's actually no reason why I can't fetch the data I need from the database, then put every single one of them into an array in the order I want and then just send that array as a json object. I just thought there would be a way to add an object node to where ever I wanted. You might be right, this just isn't a good way to do this, maybe I'd be better of with arrays and sending them as json instead.
Stefan Konno
+1  A: 

Objects simply don't have indexes, they have properties, and $obj->property is always $obj->property - no ordering, no indexes nothing like that.

What you need here is (generally) a numerically indexed array, not an object, and not an associative array - if you insist you need an alpha sorted associative array (alpha keys rather than numerical keys) then you can use ksort to handle this particular problem.

$array = array( 
  "a" => "value",
  "e" => "fifth value",
  "b" => "value",
  "c" => "value",
  "d" => "value",
);
ksort($array);
print_r($array);

note you can always turn it in to an object if for some reason you need this by doing $obj = (object)$array; after the sort

nathan
ksort won't help since they're not alphabetically ordered, the properties. I just made them that way in my example. But I realise what you're saying about the object not having indexes but properties, and I think I'm going about this the wrong way. The databas returns an object and I only need a few of them in a specific order, so I'm gonna turn them into an array and just send the data I need as json anyway, this will solve my problem. Thank you for your clarification.
Stefan Konno