从 Foreach Loop PHP 创建关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3833876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:16:04  来源:igfitidea点击:

Create associative array from Foreach Loop PHP

phpforeach

提问by tmartin314

I have this foreach loop:

我有这个 foreach 循环:

foreach($aMbs as $aMemb){
    $ignoreArray = array(1,3);
    if (!in_array($aMemb['ID'],$ignoreArray)){ 
        $aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
    }
}

This prints out the right fields but they are arrays inside arrays. I need the foreach loop to output a simple array like this one:

这会打印出正确的字段,但它们是数组内的数组。我需要 foreach 循环来输出一个像这样的简单数组:

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

What am I doing wrong?

我究竟做错了什么?

回答by GWW

You need to change your $aMemberships assignment

您需要更改您的 $aMemberships 分配

$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.

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

回答by ShivarajRH

Associative array in foreach statement:

foreach 语句中的关联数组:

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

  $field_data[$field]=$value;

}

Output:

输出:

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

insertion in CodeIgniter:

在 CodeIgniter 中插入:

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

回答by Tim Gautier

Instead of

代替

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

Try

尝试

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

回答by codaddict

Your existing code uses incremental key and uses the array as corresponding value. To make make $aMembershipsan associative array with key as $aMemb['ID']and value being $aMemb['Name']you need to change

您现有的代码使用增量键并将数组用作相应的值。要$aMemberships使用键作为$aMemb['ID']和值创建关联数组,$aMemb['Name']您需要更改

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

in the foreach loop to:

在 foreach 循环中:

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

回答by mklfarha

it prints an array of arrays because you are doing so in this line

它打印一个数组数组,因为您在这一行中这样做

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

where you [] after a variable your are indicating to assign the value in a new row of the array and you are inserting an other array into that row

您 [] 在一个变量之后,您指示在数组的新行中分配值,并且您将另一个数组插入该行

so you can use the the examples the others haver already gave or you can use this method:

所以你可以使用其他人已经给出的例子,或者你可以使用这种方法:

int array_push ( array &$array , mixed $var [, mixed $... ] )

here is an example that you can find in the api

这是您可以在 api 中找到的示例

<?php
$stack = array(0=>"orange",1=>"banana");
array_push($stack, 2=>"apple",3=>"raspberry");
print_r($stack);
?>

//prints
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

http://php.net/manual/en/function.array-push.php

http://php.net/manual/en/function.array-push.php

回答by Gufran Hasan

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

key and value在 foreach 循环中获得一个关联数组,并创建一个具有键和值对的关联数组。

$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');