Convert a PHP object to an associative array

后端 未结 30 1534
走了就别回头了
走了就别回头了 2020-11-22 02:18

I\'m integrating an API to my website which works with data stored in objects while my code is written using arrays.

I\'d like a quick-and-dirty function to convert

30条回答
  •  感情败类
    2020-11-22 02:53

    Also you can use The Symfony Serializer Component

    use Symfony\Component\Serializer\Encoder\JsonEncoder;
    use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
    use Symfony\Component\Serializer\Serializer;
    
    $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
    $array = json_decode($serializer->serialize($object, 'json'), true);
    

提交回复
热议问题