I have this class :
class MyObject{
var $title = null;
var $description = null;
var $items = [];
var $metas = [];
var $image = null;
var $country = 'Belgium';
}
And this data :
$data = new MyObject();
$data->title = 'NEW ITEM';
$data->children = ['CHILD1','CHILD2'];
$data->image = 'image.gif';
$data->country = 'Belgium';
Before storing my data in my database, I would like to remove all the defaults values from the datas, and get this output:
$dataToStore = array(
'title'=>'NEW ITEM',
'children'=>['CHILD1','CHILD2'],
'image'=>'image.gif'
);
I made an attempts with
$blank = new MyObject();
$defaults = (array)$blank;
$dataToStore = array_diff((array)$data, (array)$blank);
But it doesn't work since I get an Array to string conversion.
How could I do ?
Thanks !
Try this:
We get default values and set in
$oneAfter set new data, we get default values and set in
$twoThen, we check which key is not changed