Testing content of object
Posted: Mon Nov 23, 2009 8:32 am
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,
$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,