1

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.