If your data isn't fitting in your schema very well, consider switching to something like MongoDB. From their docs:
Project Array Documents
A students collection contains the following documents where the grades field is an array of documents; each document contain the three field names grade, mean, and std:
{ "_id" : 7, semester: 3, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 } ] }
{ "_id" : 8, semester: 3, "grades" : [ { grade: 92, mean: 88, std: 8 },
{ grade: 78, mean: 90, std: 5 },
{ grade: 88, mean: 85, std: 3 } ] }
In the following query, the projection { "grades.$": 1 } returns only the first element with the mean greater than 70 for the grades field:
db.students.find(
{ "grades.mean": { $gt: 70 } },
{ "grades.$": 1 }
)
The operation returns the following documents:
{ "_id" : 7, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 } ] }
{ "_id" : 8, "grades" : [ { "grade" : 92, "mean" : 88, "std" : 8 } ] }
Written in PHP, this query would look like:
$mongoClient = new MongoClient("mongodb://localhost");
$students = $mongoClient->selectCollection("db", "students");
$query = array("grades.mean" => array('$gt' => 70));
$fields = array("grades.$" => 1);
$result = $students->find($query, $fields);
foreach($result as $grades) {
print_r($grades);
}