php 如何使用键值对创建数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1138359/
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 01:12:11  来源:igfitidea点击:

How can I create an array with key value pairs?

phparrays

提问by sanders

How can I add key value pairs to an array?

如何将键值对添加到数组中?

This won't work:

这行不通:

public function getCategorieenAsArray(){

    $catList = array();

    $query = "SELECT DISTINCT datasource_id, title FROM table";
    if ($rs=C_DB::fetchRecordset($query)) {
        while ($row=C_DB::fetchRow($rs)) {
            if(!empty($row["title"])){
                array_push($catList, $row["datasource_id"] ."=>". $row["title"] );
            }
        }
     }

    return($catList);
}

Because it gives me:

因为它给了我:

Array ( [0] => 1=>Categorie 1 [1] => 5=>Categorie 2 [2] => 2=>Caterorie 2 ) 

And I expect:

我期望:

Array ( [1] =>Categorie 1 [5] => Categorie 2  ) 

回答by joy

$data =array();
$data['user_code']  = 'JOY' ;
$data['user_name']  = 'JOY' ;
$data['user_email'] = '[email protected]';

回答by Gumbo

Use the square bracket syntax:

使用方括号语法

if (!empty($row["title"])) {
    $catList[$row["datasource_id"]] = $row["title"];
}

$row["datasource_id"]is the key for where the value of $row["title"]is stored in.

$row["datasource_id"]是值$row["title"]存储位置的键。

回答by WCWedin

My PHP is a little rusty, but I believe you're looking for indexed assignment. Simply use:

我的 PHP 有点生疏,但我相信您正在寻找索引分配。只需使用:

$catList[$row["datasource_id"]] = $row["title"];

In PHP arrays are actually maps, where the keys can be either integers or strings. Check out PHP: Arrays - Manualfor more information.

在 PHP 中数组实际上是映射,其中的键可以是整数或字符串。查看PHP: Arrays - Manual了解更多信息。

回答by Rajan Rawal

You can create the single value array key-value as

您可以将单值数组键值创建为

$new_row = array($row["datasource_id"]=>$row["title"]);

inside while loop, and then use array_mergefunction in loop to combine the each new $new_rowarray.

在 while 循环中,然后array_merge在循环中使用函数来组合每个新$new_row数组。

回答by Mahmoud

You can use this function in your application to add keys to indexed array.

您可以在应用程序中使用此函数将键添加到索引数组。

public static function convertIndexedArrayToAssociative($indexedArr, $keys)
{
    $resArr = array();
    foreach ($indexedArr as $item)
    {
        $tmpArr = array();
        foreach ($item as $key=>$value)
        {
            $tmpArr[$keys[$key]] = $value;
        }
        $resArr[] = $tmpArr;
    }
    return $resArr;
}

回答by radhason power

No need array_push function.if you want to add multiple item it works fine. simply try this and it worked for me

不需要 array_push 函数。如果你想添加多个项目,它工作正常。试试这个,它对我有用

class line_details {
   var $commission_one=array();
   foreach($_SESSION['commission'] as $key=>$data){
          $row=  explode('-', $key);
          $this->commission_one[$row['0']]= $row['1'];            
   }

}