0

Hi i need to convert array to object.

So i use :

$object = (object)$array;

But my problem is i have not the same object. In my current object i have :

class Proxies\__CG__\...#1142 (25) { my object...

And when i convert array to string i have :

class stdClass#1394 (1) {


public ${0} =>


class Proxies\__CG__\...#1142 (25) { my object...

How i can to delete the class std and public 0 ?

Ygg69
  • 261
  • 6
  • 22
  • Why did you convert the object to an array in the first place? – Barmar Feb 07 '17 at 10:18
  • What does the array look like? When you convert an array to an object, each index of the array becomes a property name in the object. It sounds like you really want to convert `$array[0]` to an object. – Barmar Feb 07 '17 at 10:20
  • When you convert an array to an object, it always creates `stdClass`. How is it supposed to know that you want some other class? – Barmar Feb 07 '17 at 10:21
  • To convert an array to a specific class, you need to write a function that does it. – Barmar Feb 07 '17 at 10:22
  • Its in symfony, i need to set an object to my entity, and i set the before last parameters. So for get this before last parameter, i need to get an array of object, i use array_splice($array, -2, 1, true), then i convert this to object – Ygg69 Feb 07 '17 at 10:23
  • i have try $object = (object)$test[0]; and indeed its that i want, but its possible to do the same without '[n]' ? – Ygg69 Feb 07 '17 at 10:30
  • I'm not sure why you even need `(object)`, if the elements of the array are already objects. – Barmar Feb 07 '17 at 10:31
  • I can't think of any way to do it without `[n]`. That's how you get specific elements of an array. Why is that a problem? – Barmar Feb 07 '17 at 10:32
  • I don't know, how many result i can have, this time i can had 3 result, so its $array[2], but next time if i have more than 3 result i can't put $array[2] that will be wrong. Its for that i want array_slice with -2 in first paramters, and then convert it in object :/. Sorry for my bad english – Ygg69 Feb 07 '17 at 10:36
  • i can in my foreach add an integer who take the number of results, and add -1 when in my array, but if its possible i woulk like do this with other method – Ygg69 Feb 07 '17 at 10:38
  • You need to post your original data and the result you're trying to get in the question. It's becoming very unclear what you really need. – Barmar Feb 07 '17 at 10:39
  • For now i have found a solution, i put [n], i get number of result -1, for get before last result, i will try laster to found a better solution, thanks for your help – Ygg69 Feb 07 '17 at 10:49

0 Answers0