What I’m trying to do:
Obtaining the result of a find() query in Cakephp as an array of rows
Why?:
I want to send the results to Excel, so the regular cakePHP format does not suits me.
Solutions I know of:
I see two possible solutions:
- custom SQL query
- treating the returned array to fit my needs
My question:
Is there a simpler solution to get the solution in a standart format: an array with rows?
Edit:
cakephp returns arrays like this:
$data=array("0"=>array("ModelName"=>array(Model.field1,Model.field2),
"LinkedModelName"=>array(LinkedModel.field1,LinkedModel.field2)
)
I want:
$data=array("0"=>array(Model.field1,Model.field2,LinkedModel.field1,LinkedModel.field2));
Solution: (thks Dave):
foreach($data as &$row){
$row = Set::flatten($row);
}
According to the book [here], you can use
Set::flatten($data);to “Collapse a multi-dimensional array into a single dimension”.It’s example:
From there (if you need), it should be pretty simple to remove the
#.prefix on the fields.Something like this possibly:
I haven’t tested it, but it’s an accepted answer to a very similar issue here: Remove numeric prefix from string – PHP regex.