在 PHP 中创建关联数组

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

Creating Associative Array in PHP

phparraysassociative-array

提问by Wearybands

I have a multidimensional array.

我有一个多维数组。

$shop = array( 
              array("appn1", "pub1" ,"pub2" , "pub3"),
              array("appn2", "pub1"),
              array("appn3", "pub1" ,"pub2")
            ); 

The first item in each array is application numberand the rest in each array are the publication numbers. I get the first item(application number) and the last item of each array(latest publication number) like this

每个数组中的第一项是申请号,每个数组中的其余部分是出版号。我像这样得到每个数组的第一项(申请号)和最后一项(最新出版号)

 $index = count(array_keys($shop));
    for($i=0;$i<$index;$i++){

        $appln_nr = $shop[$i][0];
        echo $appln_nr;

        $publn_nr_index = count(array_keys($shop[$i]))-1;
        $publn_nr = $shop[$i][$publn_nr_index];
        echo $publn_nr;
   }

Now I have application number and publication number for each inner array.

现在我有每个内部阵列的申请号和出版号。

I want to create an associative array from the application numbers and publication numbers.

我想从申请号和出版号创建一个关联数组。

where the key should be the application number and its value is the publication number.

其中键应该是申请号,其值是公开号。

Thanks

谢谢

EDIT

编辑

What I am getting from $shop array

我从 $shop 数组中得到了什么

 Array
 (
  [0] => Array
    (
        [0] => appn1
        [1] => pub1
        [2] => pub2
        [3] => pub3
    )

  [1] => Array
    (
        [0] => appn2
        [1] => pub1
    )

  [2] => Array
    (
        [0] => appn3
        [1] => pub1
        [2] => pub2
    )
)

And this is what I need in my associative array

这就是我在关联数组中所需要的

Array(
    "appn1" => "pub3"
    "appn2" => "pub1"
    "appn3" => "pub2"
)

采纳答案by aleation

Finally i understood what you wanted, after your edit XD:

在您编辑 XD 之后,我终于明白了您想要什么:

$shop = array(
    array("appn1", "pub1" ,"pub2" , "pub3"),
    array("appn2", "pub1"),
    array("appn3", "pub1" ,"pub2")
);
$shopNew = array();

foreach($shop as $value){
    $shopNew[$value[0]] = end($value);
}

// now if you want you can replace $shop and unset $shopNew
$shop = $shopNew;
unset($shopNew);    

print_r($shop); 

the output is this:

输出是这样的:

Array (
  [appn1] => pub3
  [appn2] => pub1
  [appn3] => pub2
)

回答by hakre

You can easily convert your array into a new format by using the first element as key (see reset) and the last element (see end) as value:

通过使用第一个元素作为键(参见reset)和最后一个元素(参见end)作为值,您可以轻松地将数组转换为新格式:

foreach($shop as $fl) {
    $v[reset($fl)] = end($fl);
}

Result is in $vthen.

结果在$v那时。

If you want to transform the array you need to delete each element as well:

如果要转换数组,还需要删除每个元素:

foreach($shop as $v => $fl) {
    $shop[reset($fl)] = end($fl);
    unset($shop[$v]);
}

Result is in $shopthen. Unset takes care of removing from the array.

结果在$shop那时。Unset 负责从数组中删除。

Output in both cases is:

两种情况下的输出都是:

array(3) {
  'appn1' =>
  string(4) "pub3"
  'appn2' =>
  string(4) "pub1"
  'appn3' =>
  string(4) "pub2"
}

回答by Baba

You can try

你可以试试

$shop = array(
        array("appn1","pub1","pub2","pub3"),
        array("appn2","pub1"),
        array("appn3","pub1","pub2")
        );

$final = array();
array_map(function ($var) use(&$final) {$final[reset($var)] = end($var);}, $shop);
var_dump($final);

Output

输出

array
  'appn1' => string 'pub3' (length=4)
  'appn2' => string 'pub1' (length=4)
  'appn3' => string 'pub2' (length=4)

回答by Hitesh Tarbundiya

You can also create like this,

你也可以这样创建,

$arrField = [];
$arrField['id'] = '0001';
$arrField["first_name"] ='Demo Test';
print_r($arrField);

print_r($arrField) display output like this.

print_r($arrField) 像这样显示输出。

Array ( [id] => 0001 [first_name] => Demo Test )

回答by Teena Thomas

try this:

尝试这个:

 foreach($shop as $k => $v) {            
     $new_arr[$v[0]] = end($v);      
 }

It should give you this result,

它应该给你这个结果,

$new_arr = array(
      "appn1" => "pub3",
      "appn2" => "pub1",
      "appn3" => "pub2"-
);