从二维数组的两列创建平面关联数组并过滤掉黑名单值

发布于 2024-09-25 14:25:41 字数 707 浏览 0 评论 0原文

我使用以下 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 技术交流群。

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

发布评论

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

评论(6

懒猫 2024-10-02 14:25:41

如果你想要一个地图,你需要更改你的 $aMemberships 分配

$aMemberships[] = $aMemb['Name']; 

如果你想要一个数组,

$aMemberships[$aMemb['ID']] = $aMemb['Name'];

您正在做的是将一个数组附加到一个数组。

You need to change your $aMemberships assignment

$aMemberships[] = $aMemb['Name']; 

If you want an array

$aMemberships[$aMemb['ID']] = $aMemb['Name'];

if you want a map.

What you are doing is appending an array to an array.

落日海湾 2024-10-02 14:25:41

foreach 语句中的关联数组:

foreach($nodeids as $field => $value) {

  $field_data[$field]=$value;

}

输出:

Array(
$field => $value,
$field => $value
...
);

在 CodeIgniter 中插入:

$res=$this->db->insert($bundle_table,$field_data);

Associative array in foreach statement:

foreach($nodeids as $field => $value) {

  $field_data[$field]=$value;

}

Output:

Array(
$field => $value,
$field => $value
...
);

insertion in CodeIgniter:

$res=$this->db->insert($bundle_table,$field_data);
黑色毁心梦 2024-10-02 14:25:41

而不是

$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);

尝试

$aMemberships[$aMemb['ID']] = $aMemb['Name'];

Instead of

$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);

Try

$aMemberships[$aMemb['ID']] = $aMemb['Name'];
如若梦似彩虹 2024-10-02 14:25:41

您现有的代码使用增量键并使用数组作为相应的值。
要使 $aMemberships 成为一个关联数组,键为 $aMemb['ID'] 且值为 $aMemb['Name'] 您需要

    $aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);

将 foreach 循环更改为:

    $aMemberships[$aMemb['ID']] = $aMemb['Name']);

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 change

    $aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);

in the foreach loop to:

    $aMemberships[$aMemb['ID']] = $aMemb['Name']);
唐婉 2024-10-02 14:25:41

您可以在 foreach 循环中获取关联数组的键和值,并使用键和值对创建关联。

$aMemberships=array();//define array
foreach($aMbs as $key=>$value){
    $ignoreArray = array(1,3);
    if (!in_array($key,$ignoreArray)){ 
        $aMemberships[$key] = $value;
    }
}

它会给你一个预期的输出:

array('1' => 'Standard', '2' => 'Silver');

You get key and value of an associative array in foreach loop and create an associative with key and value pairs.

$aMemberships=array();//define array
foreach($aMbs as $key=>$value){
    $ignoreArray = array(1,3);
    if (!in_array($key,$ignoreArray)){ 
        $aMemberships[$key] = $value;
    }
}

It will give you an expected output:

array('1' => 'Standard', '2' => 'Silver');
等待圉鍢 2024-10-02 14:25:41

您可以使用 array_column() 创建一个新的关联数组,然后使用 array_diff_key() 过滤掉列入黑名单的键,而不是在循环的每次迭代中执行过滤过程。翻转黑名单。这样做的优点是进程将始终只进行 3 次函数调用,而不是 1 + n 次函数调用。

代码:(演示)

$aMbs = [
    ['ID' => 1, 'Name' => 'Standard'],
    ['ID' => 2, 'Name' => 'Silver'],
    ['ID' => 3, 'Name' => 'Gold'],
    ['ID' => 4, 'Name' => 'Platinum'],
    ['ID' => 5, 'Name' => 'Unobtainium'],
];

$ignoreArray = [1, 3];

var_export(
    array_diff_key(
        array_column($aMbs, 'Name', 'ID'),
        array_flip($ignoreArray)
    )
);

输出:

array (
  2 => 'Silver',
  4 => 'Platinum',
  5 => 'Unobtainium',
)

如果您希望保留 foreach()循环,那么前面的答案就完全没问题。这是使用数组解构的 foreach() 样式。这种语法的优点(如果您有其他不相关的列)是您不需要访问每行中的所有数据,并且可以设置方便的临时变量。 演示

$ignoreArray = [1, 3];
$aMemberships = [];
foreach ($aMbs as ['ID' => $id, 'Name' => $name]) {
    if (!in_array($id, $ignoreArray)) {
        $aMemberships[$id] = $name;
    }
}
var_export($aMemberships);
// same output as the previous snippet

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 with array_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)

$aMbs = [
    ['ID' => 1, 'Name' => 'Standard'],
    ['ID' => 2, 'Name' => 'Silver'],
    ['ID' => 3, 'Name' => 'Gold'],
    ['ID' => 4, 'Name' => 'Platinum'],
    ['ID' => 5, 'Name' => 'Unobtainium'],
];

$ignoreArray = [1, 3];

var_export(
    array_diff_key(
        array_column($aMbs, 'Name', 'ID'),
        array_flip($ignoreArray)
    )
);

Output:

array (
  2 => 'Silver',
  4 => 'Platinum',
  5 => 'Unobtainium',
)

If you'd rather keep the foreach() loop, then the earlier answers are perfectly fine. Here's a foreach() 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

$ignoreArray = [1, 3];
$aMemberships = [];
foreach ($aMbs as ['ID' => $id, 'Name' => $name]) {
    if (!in_array($id, $ignoreArray)) {
        $aMemberships[$id] = $name;
    }
}
var_export($aMemberships);
// same output as the previous snippet
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文