使用一列作为键、另一列作为值从行数组生成关联数组

发布于 2024-09-18 21:28:25 字数 463 浏览 4 评论 0原文

我有一个 MySQL 结果集,每行有 2 个值。

每次循环这些结果时,我都想将它们添加到一个数组中。

我希望一个值作为键,另一个作为数组值。

我尝试了这个,但它似乎不起作用:

$dataarray[] = $row['id'] => $row['data'];

如果我有:

$resultSet = [
    ['id' => 1, 'data' => 'one'],
    ['id' => 2, 'data' => 'two'],
    ['id' => 3, 'data' => 'three']
];

我想生成:

[
    1 => 'one',
    2 => 'two',
    3 => 'three'
]

I have a MySQL result set with 2 values in each row.

Each time I loop through these results, I want to add them to an array.

I want one value to be the key, and the other to be the array value.

I tried this, but it doesn't seem to work:

$dataarray[] = $row['id'] => $row['data'];

If I have:

$resultSet = [
    ['id' => 1, 'data' => 'one'],
    ['id' => 2, 'data' => 'two'],
    ['id' => 3, 'data' => 'three']
];

I want to generate:

[
    1 => 'one',
    2 => 'two',
    3 => 'three'
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-09-25 21:28:25

为什么不直接使用呢

$dataarray[$row['id']] = $row['data'];

Why not just use

$dataarray[$row['id']] = $row['data'];

?

热情消退 2024-09-25 21:28:25
$dataarray[ $row['id'] ] = $row[ 'data' ];
$dataarray[ $row['id'] ] = $row[ 'data' ];
迷途知返 2024-09-25 21:28:25

对于此任务,使用 array_column() 而不是 foreach() 更加优雅/富有表现力/现代/简洁。

另外,array_combine(array_column($array, 'id'), array_column($array, 'data')) 工作得太辛苦,永远不应该使用。

第一个参数是输入行数组。
第二个参数是要成为输出数组中的值的列。
第三个参数是要成为输出数组中的键的列。

代码:(Demo)

$array = [
    ['id' => 1, 'key' => 'foo', 'data' => 'a'],
    ['id' => 2, 'key' => 'bar', 'data' => 'b'],
    ['id' => 3, 'key' => 'barf', 'data' => 'c'],
];

var_export(
    array_column($array, 'data', 'id')
);

输出:

array (
  1 => 'a',
  2 => 'b',
  3 => 'c',
)

调用集合的 Laravel 等效方法是 pluck()< /code> 类似:

$collection->pluck('data', 'id')

如果您想分配新的一级键但保持行不变,则可以将 null 写入本机函数调用的第二个参数。

代码:(演示)

var_export(
    array_column($array, null, 'id')
);

输出:

array (
  1 => 
  array (
    'id' => 1,
    'key' => 'foo',
    'data' => 'a',
  ),
  2 => 
  array (
    'id' => 2,
    'key' => 'bar',
    'data' => 'b',
  ),
  3 => 
  array (
    'id' => 3,
    'key' => 'barf',
    'data' => 'c',
  ),
)

一种较少实现、无功能的技术是在 无体 foreach() 循环。 (演示

$array = [
    ['id' => 1, 'data' => 'a'],
    ['id' => 2, 'data' => 'b'],
    ['id' => 3, 'data' => 'c'],
];

$result = [];
foreach ($array as ['id' => $id, 'data' => $result[$id]]);
var_export($result);

这相当于之前建议的答案:

foreach ($array as $row) {
    $result[$row['id']] = $row['data'];
}

输出:

array (
  1 => 'a',
  2 => 'b',
  3 => 'c',
)

It is more elegant/expressive/modern/concise to use array_column() instead of a foreach() for this task.

Also, array_combine(array_column($array, 'id'), array_column($array, 'data')) is working too hard and should never be used.

The first parameter is the input array of rows.
The second parameter is the column to become the values in the output array.
The third parameter is the column to become the keys in the output array.

Code: (Demo)

$array = [
    ['id' => 1, 'key' => 'foo', 'data' => 'a'],
    ['id' => 2, 'key' => 'bar', 'data' => 'b'],
    ['id' => 3, 'key' => 'barf', 'data' => 'c'],
];

var_export(
    array_column($array, 'data', 'id')
);

Output:

array (
  1 => 'a',
  2 => 'b',
  3 => 'c',
)

The Laravel equivalent method to call on a collection is pluck() like:

$collection->pluck('data', 'id')

If you'd like to assign new first level keys but leave the rows unchanged, you write null as the second parameter of the native function call.

Code: (Demo)

var_export(
    array_column($array, null, 'id')
);

Output:

array (
  1 => 
  array (
    'id' => 1,
    'key' => 'foo',
    'data' => 'a',
  ),
  2 => 
  array (
    'id' => 2,
    'key' => 'bar',
    'data' => 'b',
  ),
  3 => 
  array (
    'id' => 3,
    'key' => 'barf',
    'data' => 'c',
  ),
)

A lesser realized, functionless technique would be to use array destructuring inside a body-less foreach() loop. (Demo)

$array = [
    ['id' => 1, 'data' => 'a'],
    ['id' => 2, 'data' => 'b'],
    ['id' => 3, 'data' => 'c'],
];

$result = [];
foreach ($array as ['id' => $id, 'data' => $result[$id]]);
var_export($result);

This is equivalent to the earlier answers which advise this:

foreach ($array as $row) {
    $result[$row['id']] = $row['data'];
}

Output:

array (
  1 => 'a',
  2 => 'b',
  3 => 'c',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文