Reputation: 39
I have two arrays
The array $groeperingen
contains the short codes and the full names and the array $groep cointains
only shortcodes.
What I want is that all short codes in $groep
are replaced with the full name.
The array $groepering
looks like this:
Array
(
[019] => Regio 019a
[013] => Regio 013
[011] => Regio alpha
[AR] => ArmUsers
[CU] => ComputerUsers
[GA] => Gamers
[OP] => Opensource
)
The array $groep
looks like this:
Array
(
[0] => CU
[1] => GA
[2] => OP
)
How can I do this?
Upvotes: 1
Views: 50
Reputation: 10617
This will create a new array.
$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);
Upvotes: 0
Reputation: 2841
Use array_map to apply a function to each element of the $groep
array.
<?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);
The content of $result
is:
Array
(
[0] => ComputerUsers
[1] => Gamers
[2] => Opensource
)
See it working here: http://phpfiddle.org/main/code/7sv-1kp
Upvotes: 2