1

我有两个数组

该数组$groeperingen包含短代码和全名,而数组$groep cointains仅包含短代码。

我想要的是所有短代码$groep都替换为全名。

数组$groepering如下所示:

Array
(
    [019] => Regio 019a
    [013] => Regio 013
    [011] => Regio alpha
    [AR] => ArmUsers
    [CU] => ComputerUsers
    [GA] => Gamers
    [OP] => Opensource
)

数组$groep如下所示:

Array
(
    [0] => CU
    [1] => GA
    [2] => OP
)

我怎样才能做到这一点?

4

2 回答 2

2

使用array_map将函数应用于$groep数组的每个元素。

<?php

$groep = Array(
   "CU",
   "GA",
   "OP"
);
$groepering = Array(
   "019" => "Regio 019a",
   "013" => "Regio 013",
   "011" => "Regio alpha",
   "AR" => "ArmUsers",
   "CU" => "ComputerUsers",
   "GA" => "Gamers",                
   "OP" => "Opensource"
);

$result = array_map(function ($x) use ($groepering) {       
   return $groepering[$x];
}, $groep);

print_r($result);

的内容$result是:

Array
(
    [0] => ComputerUsers
    [1] => Gamers
    [2] => Opensource
)

看到它在这里工作:http: //phpfiddle.org/main/code/7sv-1kp

于 2013-08-23T23:53:07.367 回答
0

这将创建一个新数组。

$groeperingen = array(
                  '019' => 'Regio 019a',
                  '013' => 'Regio 013',
                  '011' => 'Regio alpha',
                  'AR' => 'ArmUsers',
                  'CU' => 'ComputerUsers',
                  'GA' => 'Gamers',
                  'OP' => 'Opensource'
                );
$groep = array('CA', 'GA', 'OP');

function changeValue($ary, $basedOnAry){
  foreach($ary as $i => $v){
    $a[$i] = $basedOnAry[$v];
  }
  return $a;
}

$newArray = changeValue($groep, $groeperingen);
于 2013-08-24T00:07:03.720 回答