Skip to content
wordpress meta

title: 'php-stdclass-object-vs-array'
date: '2023-12-02T10:02:02-05:00'
status: publish
permalink: /php-stdclass-object-vs-array
author: admin
excerpt: ''
type: post
id: xxxx
category:
    - 'php'
tag: ['code','array']
post_format: []

php-stdclass-object-vs-array.md

Sometimes you may prefer handling your data like an object instead of an array. I prefer object style when I am working with code handling json from API's etc..

So instead of the $my_array["field1"] notation you can handle it as $my_array->field1.

Its pretyy simple just cast your array variable like this:

$data = (object) $my_array;

Better write up here: https://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/