I can't seem to find a proper way to do the following:
I'm selecting values from * tables with joins, let's say:
SELECT templates.id as templates-id, template_fields.id as template_fields-id FROM templates JOIN template_fields ON templates.id = template_fields.template_id.
The result I get is the following:
Array
(
[0] => Array
(
[template_id] => 1
[template_field_id] => 20
)
[1] => Array
(
[template_id] => 1
[template_field_id] => 21
)
[2] => Array
(
[template_id] => 2
[template_field_id] => 22
)
[3] => Array
(
[template_id] => 2
[template_field_id] => 23
)
)
But I need to have something like this:
Array
(
[0] => Array
(
[id] => 1
[template_fields] => Array
(
[0] => Array
(
[id] => 20
)
[1] => Array
(
[id] => 21
)
)
)
[1] => Array
(
[id] => 2
[template_fields] => Array
(
[0] => Array
(
[id] => 22
)
[1] => Array
(
[id] => 23
)
)
)
)
The class that's responsible for building these queries remembers the order of the joins. The first parameter in the columnalias (templates, template_fields) can be used to know which one has to be the subarray.
Here's how the class is used:
$result = $database->select('templates')->join('templatesfields')->getResult()->formattedOutput('');
The formatted output is something like this:
$formattedArray = array();
foreach ($this->result as $row)
{
$ids = array();
$tempColumnValuesArray = array();
foreach ($row as $columnKey => $value)
{
$columnKeyExploded = explode('-', $columnKey);
$columnTable = $columnKeyExploded[0];
$columnColumnName = $columnKeyExploded[1];
if (!isset($formattedArray[$columnTable]))
{
$formattedArray[$columnTable] = array();
}
if ($columnColumnName === 'id' && !isset($formattedArray[$columnTable][$value]))
{
$formattedArray[$columnTable][$value] = array();
$ids[$columnTable] = $value;
}
if (!isset($ids[$columnTable]))
{
$tempColumnValuesArray[$columnColumnName] = $value;
}
else
{
if ($tempColumnValuesArray)
{
array_merge($formattedArray[$columnTable][$ids[$columnTable]], $tempColumnValuesArray);
$tempColumnValuesArray = array();
}
$formattedArray[$columnTable][$ids[$columnTable]][$columnColumnName] = $value;
}
}
}
foreach ($formattedArray as $tableName => $rows)
{
/**
* TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP
*/
if ($tableName == 'templates')
{
continue;
}
else
{
foreach ($rows as $row)
{
$formattedArray['templates'][$row['template_id']]['templates_fields'][] = $row;
}
}
}
return $formattedArray;
But this function can't use static parameters...
Is there any way to convert this flat array to a multidimensional array like the given example?
Many thanks in advance!
$arr = Array
(
0 => Array
(
'template_id' => 1,
'template_field_id' => 20
),
1 => Array
(
'template_id' => 1,
'template_field_id' => 21
),
2 => Array
(
'template_id' => 2,
'template_field_id' => 22
),
3 => Array
(
'template_id' => 2,
'template_field_id' => 23
)
);
$newarr = array();
foreach($arr as $key=>$val){
$newarr[$val['template_id']][] = $val;
}
$i=0;
foreach($newarr as $k=>$v){
foreach($v as $m){
$newarr1[$i]['id']= $m['template_id'];
$newarr1[$i]['template_field_id'][]['id']= $m['template_field_id'];
}
$i++;
}
echo "<pre>"; print_r($newarr1);
You can use array_column and array_search to search inside multidimension result array.
$arr = Array
(
0 => Array
(
'template_id' => 1,
'template_field_id' => 20
),
1 => Array
(
'template_id' => 1,
'template_field_id' => 21
),
2 => Array
(
'template_id' => 2,
'template_field_id' => 22
),
3 => Array
(
'template_id' => 2,
'template_field_id' => 23
)
);
$resultArr = array();
foreach($arr as $data){
$index = array_search($data['template_id'], array_column($resultArr, 'id'));
if($index !== false){
if(is_array($resultArr[$index]["template_fields"]))
array_push($resultArr[$index]["template_fields"],array("id"=>$data["template_field_id"]));
else
$resultArr[$index]["template_fields"] = array(array("id"=>$data["template_field_id"]));
}else{
$newData = array("id"=>$data['template_id'],"template_fields"=>array(array("id"=>$data["template_field_id"])));
array_push($resultArr,$newData);
}
}
print_r($resultArr);