php 按列对查询结果数据进行分组,并为每个组中所有缺少的列设置默认值

58wvjzkj  于 2022-12-10  发布在  PHP
关注(0)|答案(3)|浏览(106)

我创建了一个创建项目列表的方法,但并不是每个值都被填充。我尝试了几种方法,但都没有解决这个问题。我尝试了if else语句,但它没有解决这个问题。我得到的值仍然比预期的少。有人能给予我一个提示或重写我的方法吗,这样我就可以回到我的项目了。
方法:

public function getDataListItems(int $category, array $list) 
{
    global $dbh;

    $query = 'SELECT data.value, data.uid, fields.name 
              FROM data 
                JOIN fields ON data.field_id = fields.id 
              WHERE fields.category_id = "' . trim($category) . 
            '" ORDER BY uid';

    $sql = $dbh->prepare($query);
    $sql->execute();
    $values = $sql->fetchAll(PDO::FETCH_ASSOC);

    $data = '';
    $items = [];
    foreach ($values as $value) {
        foreach ($list as $key) {
            if(!empty($data)){
                if($data == $value['uid']) {
                    if($key == $value['name']) {
                        $item = [
                            $value['name'] => $value['value'],
                            'uid' => $value['uid'],
                        ];
                    }else{
                        $item = [
                            $key => '',
                            'uid' => $value['uid'],
                        ];          
                    }
                    $items[$value['uid']] = array_merge($items[$value['uid']], $item);
                }else{
                    if($key == $value['name']) {
                        $items[$value['uid']] = [
                            $value['name'] => $value['value'],
                            'uid' => $value['uid'],
                        ];  
                    }else{
                        $items[$value['uid']] = [
                            $key => '',
                            'uid' => $value['uid'],
                        ];          
                    }       
                }
            }else{
                if($key == $value['name']) {
                    $items[$value['uid']] = [
                        $value['name'] => $value['value'],
                        'uid' => $value['uid'],
                    ];  
                }else{
                    $items[$value['uid']] = [
                        $key => '',
                        'uid' => $value['uid'],
                    ];          
                }       
            }
            $data = $value['uid'];
        }
    }
    return $items;  
}

$list中,您将找到:

array(3) {
  ["Voornaam"]=> string(8) "Voornaam"
  ["Achternaam"]=> string(10) "Achternaam"
  ["Initialen"]=> string(9) "Initialen"
}

$values中,您将找到:

array(7) {
  [0]=>
  array(3) {
    ["value"]=> string(7) "Bettina"
    ["uid"]=> string(15) "7d1f4f8e906245f"
    ["name"]=> string(8) "Voornaam"
  }
  [1]=>
  array(3) {
    ["value"]=> string(3) "Les"
    ["uid"]=> string(15) "7d1f4f8e906245f"
    ["name"]=> string(10) "Achternaam"
  }
  [2]=>
  array(3) {
    ["value"]=> string(6) "Simone"
    ["uid"]=> string(15) "7d1f4f8e906245g"
    ["name"]=> string(8) "Voornaam"
  }
  [3]=>
  array(3) {
    ["value"]=> string(4) "Yül"
    ["uid"]=> string(15) "7d1f4f8e906245l"
    ["name"]=> string(10) "Achternaam"
  }
  [4]=>
  array(3) {
    ["value"]=> string(6) "Joshua"
    ["uid"]=> string(15) "7d1f4f8e906245s"
    ["name"]=> string(8) "Voornaam"
  }
  [5]=>
  array(3) {
    ["value"]=> string(3) "Mas"
    ["uid"]=> string(15) "7d1f4f8e906245s"
    ["name"]=> string(10) "Achternaam"
  }
  [6]=>
  array(3) {
    ["value"]=> string(5) "Hello"
    ["uid"]=> string(15) "gGcYEJdRYJ1vqcn"
    ["name"]=> string(10) "Achternaam"
  }
}

我得到的回报是:

["7d1f4f8e906245f"]=>
  array(4) {
    ["Voornaam"]=> string(0) ""
    ["uid"]=> string(15) "7d1f4f8e906245f"
    ["Achternaam"]=> string(3) "Les"
    ["Initialen"]=> string(0) ""
  }
["7d1f4f8e906245g"]=>
  array(4) {
    ["Voornaam"]=> string(6) "Simone"
    ["uid"]=> string(15) "7d1f4f8e906245g"
    ["Achternaam"]=> string(0) ""
    ["Initialen"]=> string(0) ""
  }
["7d1f4f8e906245l"]=>
  array(4) {
    ["Voornaam"]=> string(0) ""
    ["uid"]=> string(15) "7d1f4f8e906245l"
    ["Achternaam"]=> string(4) "Yül"
    ["Initialen"]=> string(0) ""
  }
  ["7d1f4f8e906245s"]=>
  array(4) {
    ["Voornaam"]=> string(0) ""
    ["uid"]=> string(15) "7d1f4f8e906245s"
    ["Achternaam"]=> string(3) "Mas"
    ["Initialen"]=> string(0) ""
  }
  ["gGcYEJdRYJ1vqcn"]=>
  array(4) {
    ["Voornaam"]=> string(0) ""
    ["uid"]=> string(15) "gGcYEJdRYJ1vqcn"
    ["Achternaam"]=> string(5) "Hello"
    ["Initialen"]=> string(0) ""
  }
}

我所期望的结果数组:

["7d1f4f8e906245f"]=>
  array(4) {
    ["Voornaam"]=> string(0) "Bettina"
    ["uid"]=> string(15) "7d1f4f8e906245f"
    ["Achternaam"]=> string(3) "Les"
    ["Initialen"]=> string(0) ""
  }
["7d1f4f8e906245g"]=>
  array(4) {
    ["Voornaam"]=> string(6) "Simone"
    ["uid"]=> string(15) "7d1f4f8e906245g"
    ["Achternaam"]=> string(0) ""
    ["Initialen"]=> string(0) ""
  }
["7d1f4f8e906245l"]=>
  array(4) {
    ["Voornaam"]=> string(0) ""
    ["uid"]=> string(15) "7d1f4f8e906245l"
    ["Achternaam"]=> string(4) "Yül"
    ["Initialen"]=> string(0) ""
  }
  ["7d1f4f8e906245s"]=>
  array(4) {
    ["Voornaam"]=> string(0) "Joshua"
    ["uid"]=> string(15) "7d1f4f8e906245s"
    ["Achternaam"]=> string(3) "Mas"
    ["Initialen"]=> string(0) ""
  }
  ["gGcYEJdRYJ1vqcn"]=>
  array(4) {
    ["Voornaam"]=> string(0) ""
    ["uid"]=> string(15) "gGcYEJdRYJ1vqcn"
    ["Achternaam"]=> string(5) "Hello"
    ["Initialen"]=> string(0) ""
  }
}

您可以看到,我在返回列表中遗漏了一些值。

dgiusagp

dgiusagp1#

您希望根据uid值对结果进行分组,并设置一些默认值--这不需要太多条件。使用isset()来确定uid是否是第一次遇到。如果是,则设置默认值。然后在以后每次遇到相同的uid时覆盖默认值。
代码:(Demo

$values = [
    ['value' => 'Bettina', 'uid' =>  '7d1f4f8e906245f', 'name' => 'Voornaam'],
    ['value' => 'Les', 'uid' =>  '7d1f4f8e906245f', 'name' =>  'Achternaam'],
    ['value' => 'Simone', 'uid' =>  '7d1f4f8e906245g', 'name' => 'Voornaam'],
    ['value' => 'Yül', 'uid' =>  '7d1f4f8e906245l', 'name' =>  'Achternaam'],
    ['value' => 'Joshua', 'uid' =>  '7d1f4f8e906245s', 'name' => 'Voornaam'],
    ['value' => 'Mas', 'uid' =>  '7d1f4f8e906245s', 'name' =>  'Achternaam'],
    ['value' => 'Hello', 'uid' =>  'gGcYEJdRYJ1vqcn', 'name' =>  'Achternaam'],
];

$list = ['Voornaam', 'Achternaam', 'Initialen'];

foreach ($values as $row) {
    if (!isset($items[$row['uid']])) {
        $items[$row['uid']] = array_fill_keys($list, '');  // if it needs to dynamically generated
        $items[$row['uid']]['uid'] = $row['uid'];
    }
    $items[$row['uid']][$row['name']] = $row['value'];
}

var_export($items);

输出量:

array (
  '7d1f4f8e906245f' => 
  array (
    'Voornaam' => 'Bettina',
    'Achternaam' => 'Les',
    'Initialen' => '',
    'uid' => '7d1f4f8e906245f',
  ),
  '7d1f4f8e906245g' => 
  array (
    'Voornaam' => 'Simone',
    'Achternaam' => '',
    'Initialen' => '',
    'uid' => '7d1f4f8e906245g',
  ),
  '7d1f4f8e906245l' => 
  array (
    'Voornaam' => '',
    'Achternaam' => 'Yül',
    'Initialen' => '',
    'uid' => '7d1f4f8e906245l',
  ),
  '7d1f4f8e906245s' => 
  array (
    'Voornaam' => 'Joshua',
    'Achternaam' => 'Mas',
    'Initialen' => '',
    'uid' => '7d1f4f8e906245s',
  ),
  'gGcYEJdRYJ1vqcn' => 
  array (
    'Voornaam' => '',
    'Achternaam' => 'Hello',
    'Initialen' => '',
    'uid' => 'gGcYEJdRYJ1vqcn',
  ),
)

说实话,如果这是我的应用程序,我会编写一个透视查询,并在sql中完成所有这些操作,以便立即返回fetchAll()值。

uubf1zoe

uubf1zoe2#

您使用了uid作为新数组的键。但是同一个uid在原始数组中出现了多次
这个7d1f4f8e906245f和这个7d1f4f8e906245s出现了两次。
因此,您的循环将用第二个示例覆盖您在结果中创建的第一个示例。
您将不得不重新考虑要使用什么作为新数组的键。

wlwcrazw

wlwcrazw3#

一个完全独立/替代的解决方案(如果您的应用程序不需要完全动态的话,我建议您这样做)是在最早的时候执行所有的处理逻辑--在sql中。
使用透视将允许您快速地从查询转到返回的结果集。
查询:(db-fiddle Demo

SELECT 
    MAX(IF(fields.name = 'Voornaam', data.value, '')) AS Voornaam,
    data.uid,
    MAX(IF(fields.name = 'Achternaam', data.value, '')) AS Achternaam,
    MAX(IF(fields.name = 'Initialen', data.value, '')) AS Initialen
FROM data 
JOIN fields ON data.field_id = fields.id 
WHERE fields.category_id = 1
GROUP BY data.uid ASC

结果集:

| Voornaam | Achternaam | Initialen | uid             |
| -------- | ---------- | --------- | --------------- |
| Bettina  | Les        |           | 7d1f4f8e906245f |
| Simone   |            |           | 7d1f4f8e906245g |
|          | Yül        |           | 7d1f4f8e906245l |
| Joshua   | Mas        |           | 7d1f4f8e906245s |
|          | Hello      |           | gGcYEJdRYJ1vqcn |

编码:

public function getDataListItems(int $category, array $list) 
{
    global $dbh;

    $query = "SELECT 
                  MAX(IF(fields.name = 'Voornaam', data.value, '')) AS Voornaam,
                  data.uid,
                  MAX(IF(fields.name = 'Achternaam', data.value, '')) AS Achternaam,
                  MAX(IF(fields.name = 'Initialen', data.value, '')) AS Initialen
              FROM data 
              JOIN fields ON data.field_id = fields.id 
              WHERE fields.category_id = ?
              GROUP BY data.uid ASC";
    $sql = $dbh->prepare($query);
    $sql->execute($category);
    return $sql->fetchAll(PDO::FETCH_ASSOC);  // this will be an indexed array of associative arrays
}

相关问题