I pass a JSON object through the POST method and manage to parse through most of the object successfully on the PHP side. While putting the data into a database record, I want to have a field indicate whether an item exists and has content. Here is an example:
$a[image]
$a[image][url]
$a[image][caption]
$a[image][height]
$a[image][width]
$a[image][[small]
$a[image][small][url]
$a[image][small][height]
$a[image][small][width]
$a[image][medium]
$a[image][medium][url]
$a[image][medium][height]
$a[image][medium][width]
If a leaf under $a[image] has a value, then I want to set a variable corresponding to $a[image] to 1, otherwise set it to 0. For example, in my JSON expression, the object key 'image' only exists if there are values associated with the keys at the leaf levels (url, height, width).
Similarly, if a leaf under $a[image][small] has a value, then I want to set a variable corresponding to $a[image][small] the same way.
In case I am not parsing the POST data correctly, here is that code:
$a = json_decode(stripslashes($_POST['data']), true);
Are there any suggestions?
Thanks,
Testing content of object
Moderator: General Moderators
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Testing content of object
I don't think you can. In your example, $a['image'] is an array containing the keys url, caption, height etc.
So, setting $a['image'] = 1 would clear the array its currently holding, effectively deleting your json data
is there a reason you need to do this? isn't the fact that $a['image']['something'] has a value an indicator in its own right?
So, setting $a['image'] = 1 would clear the array its currently holding, effectively deleting your json data
is there a reason you need to do this? isn't the fact that $a['image']['something'] has a value an indicator in its own right?
Re: Testing content of object
Thanks for looking at this, Ian.
I don't want to reassign a value to $a['image']. I want to assign the result of the test to another variable, so $a['image'] remains intact. Maybe I was not clear about that.
I was hoping there would be a systematic built-in function that could see if there are any leaves under a key. I would settle for a function that just checks to see if there is a value (which may be another associative array) corresponding to a key, as long as that value is not null.
It may be possible, in some cases, that the value is another associative array with many keys, so I am wondering if this test be accomplished without explicitly knowing the keys of the next tier associative array. I would only want to test if there are ANY keys.
I don't want to reassign a value to $a['image']. I want to assign the result of the test to another variable, so $a['image'] remains intact. Maybe I was not clear about that.
I was hoping there would be a systematic built-in function that could see if there are any leaves under a key. I would settle for a function that just checks to see if there is a value (which may be another associative array) corresponding to a key, as long as that value is not null.
It may be possible, in some cases, that the value is another associative array with many keys, so I am wondering if this test be accomplished without explicitly knowing the keys of the next tier associative array. I would only want to test if there are ANY keys.
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Testing content of object
Have a look at PHP's empty() function. It's not always a good idea as it treats certain values as 'empty', e.g. an integer of 0 is empty, but it might do what you want.