简体   繁体   English

MySQL/PHP:将两列作为关联数组作为键 => 值对返回

[英]MySQL/PHP: return two columns as an associative array as key => value pairs

I apologize if this might have been asked before, or if this isn't even possible, but here is what I am hoping to get from a particular table using MySQL.如果之前可能有人问过这个问题,或者这甚至不可能,我深表歉意,但这是我希望从使用 MySQL 的特定表中获得的内容。

Say, I have the following table:说,我有下表:

+-------------+------------------+
| property    | value            |
+-------------+------------------+
| style       | unisex           |
| color       | blue             |
| type        | small            |
+-------------+------------------+

I would like to be able to fetch the two columns property and value as an associative array from MySQL , so the end result would be:我希望能够从MySQL获取两列propertyvalue作为关联数组,因此最终结果将是:

array(
    'style' => 'unisex',
    'color' => 'blue',
    'type'  => 'small',
);

Just to provide context so people don't misinterpret what I'm asking:只是为了提供上下文,以便人们不会误解我的要求:

I already know how I can do this using PHP once I have the result back from a generic SQL statement that would return each row's columns as key => value pairs.一旦我从通用SQL语句返回结果,我就知道如何使用 PHP 执行此操作,该语句会将每行的列作为key => value对返回。 I was just wondering if it was possible to return one column's value as a key and the second value as the value to the key for each row from MySQL directly to continue with in PHP .我只是想知道是否有可能将一列的值作为键返回,将第二个值作为键的值直接从MySQL到每一行的键,以便在PHP继续。

There is no out of the box fetch mode that will give you an array indexed like that (how would it know which is the key? what if there's more than 2 columns?).没有开箱即用的获取模式可以为您提供一个这样索引的数组(它如何知道哪个是键?如果有超过 2 列怎么办?)。

You can use PDO::FETCH_NUM and array_map() to build a new array, using the first column as key and the second as value:您可以使用PDO::FETCH_NUMarray_map()构建一个新数组,使用第一列作为键,第二列作为值:

<?php
$query = $db->prepare("SELECT property, value FROM table");
$query->execute();
$row = $query->fetchAll(PDO::FETCH_NUM);
$rowFormatted = array_map(function ($el) {
    return [$el[0] => $el[1]];
}, $row);
var_dump($rowFormatted);

Sidenote: it looks like you're heading into an Entity-Attribute-Value antipattern.旁注:看起来您正在进入实体-属性-值反模式。 This will bring you problems along the road, so consider redesigning your schema.将使你的问题沿路,所以考虑重新设计架构。 Take a look at this resources to learn more:查看此资源以了解更多信息:

Old question, but thought I'd at least provide a MySQL only solution since that is what was asked for:老问题,但我想我至少会提供一个仅限 MySQL 的解决方案,因为这是要求的:

$stmt = $db->prepare('SELECT `property`, `value` FROM `table` WHERE 1');
$stmt->execute();

$results = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

$results will be formatted as you're expecting - an array with property as the key and value as the value. $results将按照您的预期进行格式化 - 一个以property为键、以value的数组。

Resource: https://www.php.net/manual/en/pdo.constants.php资源: https : //www.php.net/manual/en/pdo.constants.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM