从二维数组的两列创建平面关联数组并过滤掉黑名单值
我使用以下 foreach 循环用过滤后的数据填充新数组。
foreach ($aMbs as $aMemb) {
$ignoreArray = array(1, 3);
if (!in_array($aMemb['ID'], $ignoreArray)) {
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
}
}
问题是它生成一个二维数组,但我想要一个平面关联数组。
如果 $aMbs
具有以下数据:
[
['ID' => 1, 'Name' => 'Standard'],
['ID' => 2, 'Name' => 'Silver'],
['ID' => 3, 'Name' => 'Gold'],
['ID' => 4, 'Name' => 'Platinum'],
['ID' => 5, 'Name' => 'Unobtainium'],
]
所需的输出将是:
[
2 => 'Silver',
4 => 'Platinum',
5 => 'Unobtainium',
]
我怎样才能实现这一目标?
I am using the following foreach loop to populate a new array with filtered data.
foreach ($aMbs as $aMemb) {
$ignoreArray = array(1, 3);
if (!in_array($aMemb['ID'], $ignoreArray)) {
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
}
}
The problem is that it is producing a 2-dimensional array, but I want to have a flat, associative array.
If $aMbs
had the following data:
[
['ID' => 1, 'Name' => 'Standard'],
['ID' => 2, 'Name' => 'Silver'],
['ID' => 3, 'Name' => 'Gold'],
['ID' => 4, 'Name' => 'Platinum'],
['ID' => 5, 'Name' => 'Unobtainium'],
]
The desired output would be:
[
2 => 'Silver',
4 => 'Platinum',
5 => 'Unobtainium',
]
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你想要一个地图,你需要更改你的 $aMemberships 分配
如果你想要一个数组,
。
您正在做的是将一个数组附加到一个数组。
You need to change your $aMemberships assignment
If you want an array
if you want a map.
What you are doing is appending an array to an array.
foreach 语句中的关联数组:
输出:
在 CodeIgniter 中插入:
Associative array in foreach statement:
Output:
insertion in CodeIgniter:
而不是
尝试
Instead of
Try
您现有的代码使用增量键并使用数组作为相应的值。
要使
$aMemberships
成为一个关联数组,键为$aMemb['ID']
且值为$aMemb['Name']
您需要将 foreach 循环更改为:
Your existing code uses incremental key and uses the array as corresponding value.
To make make
$aMemberships
an associative array with key as$aMemb['ID']
and value being$aMemb['Name']
you need to changein the foreach loop to:
您可以在 foreach 循环中获取关联数组的
键和值
,并使用键和值对创建关联。它会给你一个预期的输出:
You get
key and value
of an associative array in foreach loop and create an associative with key and value pairs.It will give you an expected output:
您可以使用
array_column()
创建一个新的关联数组,然后使用array_diff_key()
过滤掉列入黑名单的键,而不是在循环的每次迭代中执行过滤过程。翻转黑名单。这样做的优点是进程将始终只进行 3 次函数调用,而不是 1 + n 次函数调用。代码:(演示)
输出:
如果您希望保留
foreach()
循环,那么前面的答案就完全没问题。这是使用数组解构的foreach()
样式。这种语法的优点(如果您有其他不相关的列)是您不需要访问每行中的所有数据,并且可以设置方便的临时变量。 演示Instead of performing a filtering process on each iteration of your loop, you can create a new associative array with
array_column()
, then filter out the blacklisted keys witharray_diff_key()
after flipping the blacklist. The advantage in doing so is that the process will always only take 3 function calls instead of 1 + n function calls.Code: (Demo)
Output:
If you'd rather keep the
foreach()
loop, then the earlier answers are perfectly fine. Here's aforeach()
style using array destructuring. The advantage of this syntax (if you have additional irrelevant columns), is that you don't need to access all data in each row and you set up convenient temporary variables. Demo