将具有连接的Mysql select语句转换为mulidimensional数组

时间:2017-04-20 14:46:09

标签: php mysql arrays

我似乎找不到合适的方法来执行以下操作:

我正在从带有连接的*表中选择值,让我们说:

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.

我得到的结果如下:

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
        )

)

但我需要这样的事情:

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
                        )

                )

        )

)

负责构建这些查询的类会记住联接的顺序。 columnalias中的第一个参数(templates,template_fields)可用于知道哪一个必须是子数组。

以下是该课程的使用方法:

$result = $database->select('templates')->join('templatesfields')->getResult()->formattedOutput('');

格式化的输出是这样的:

$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;

但是这个函数不能使用静态参数...

有没有办法将此平面数组转换为多维数组,如给定的示例?

非常感谢提前!

2 个答案:

答案 0 :(得分:1)

$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);

Demo

答案 1 :(得分:0)

您可以使用array_column和array_search在multidimension结果数组中搜索。

$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);

DEMO