Lets play with array values automation snippets

Hello folks, in day today life we are playing with array. Sometimes we want the huge array in copy paste format. I will explain it with examples in further post. I am going to provide you some php code snippets here. So lets play with array values automation.

Create key-value pair array with just values:

Suppose, you just have array with values, but not keys. You have to provide unique keys to each value of array without any space, no capital letters and no special characters allow except underscore for separation. Look at the following snippet.


// Your original array
$options = array('New', 'Open', 'Dead / Lost', 'Closed, Lost', 'Closed Won');

// New array with key values you want
$newOptions = array();

foreach($options as $opt)
  // Remove spaces with hypens
  $newOpt = strtolower(str_replace(' ', '-', $opt));
  // Remove all special characters
  $newOpt = preg_replace('/[^A-Za-z0-9\-]/', '', $newOpt);
  // Remove double hypen entries and replace them with underscore
  $newOpt = preg_replace('/-+/', '_', $newOpt);
  // echo $newOpt."<br>";
  if(!in_array( $newOpt, $newOptions)) {
    $newOptions[$newOpt] = $opt;
    echo '"'.$newOpt.'" => "'.$opt.'", <br>';

echo '<br>';


"new" => "New",
"open" => "Open",
"dead_lost" => "Dead / Lost",
"closed_lost" => "Closed, Lost",
"closed_won" => "Closed Won",

Array ( [new] => New [open] => Open [dead_lost] => Dead / Lost [closed_lost] => Closed, Lost [closed_won] => Closed Won )

Sometimes, you have to copy and paste the generated array in your code, that’s why I echo’ed it in the output. Sometimes, you just have generated array to use in further code, that’s why I stored it in new array.

Generate key-value pair to paste it in files with in large number:

In my recent work, once I have a scenario while working with SuiteCRM. In SuiteCRM, your dropdown options are stored in language files with key value pairs. Someone already created key value pairs and later we have to change caps to small in all the files. There are nearly 75 dropdowns having 30 to 40 options in each. I don’t have patience to change all these one by one. So I came up with following solution which help me a lot and just copy-paste-run-copy-paste cycle for all 75 dropdowns. No manual changing of smallcaps.


$array_list =array (
  '' => '',
  'App' => 'App',
  'App_issue' => 'App issue',
  'Beauty' => 'Beauty',
  'Connectivity' => 'Connectivity',
  'Dashboard' => 'Dashboard',
  'Delivery' => 'Delivery',
  'Downgrade' => 'Downgrade',
  'Eat' => 'Eat',
  'Electronic' => 'Electronic',
  'Else' => 'Else',
  'Entrainment' => 'Entrainment',
  'Gifting_telco' => 'Gifting-telco',
  'Gifting_voucher' => 'Gifting-voucher',
  'Hotel' => 'Hotel',
  'International' => 'International',
  'Migration' => 'Migration',
  'Network_coverage' => 'Network coverage',
  'Packages' => 'Packages',
  'Payment' => 'Payment',
  'Port_in' => 'Port-in'

$array = array_change_key_case($array_list,CASE_LOWER);

foreach($array as $key=>$value)
  echo '"'.$key.'" => "'.$value.'", <br>';



"" => "",
"app" => "App",
"app_issue" => "App issue",
"beauty" => "Beauty",
"connectivity" => "Connectivity",
"dashboard" => "Dashboard",
"delivery" => "Delivery",
"downgrade" => "Downgrade",
"eat" => "Eat",
"electronic" => "Electronic",
"else" => "Else",
"entrainment" => "Entrainment",
"gifting_telco" => "Gifting-telco",
"gifting_voucher" => "Gifting-voucher",
"hotel" => "Hotel",
"international" => "International",
"migration" => "Migration",
"network_coverage" => "Network coverage",
"packages" => "Packages",
"payment" => "Payment",
"port_in" => "Port-in",

Enjoy, Coding!