JustinCann
1/18/2017 - 5:17 PM

This helper takes an indexed array and turns it into a dictionary

This helper takes an indexed array and turns it into a dictionary


// This Helper takes an indexed array and turns it into a dictionary

// ie. $extra_fields = createDictionaryBasedOnValue($item->extra_fields, 'alias'); // creates array
// ie. $extra_fields = createDictionaryBasedOnValue($item->extra_fields, 'alias', 'object'); // creates object


function createDictionaryBasedOnValue($array_to_convert,$chosen_field, $object)
{

  // create new arrow
  $dictionary = array();


  foreach ($array_to_convert as $array_key => $contents) {

    $new_key = strtolower($contents->$chosen_field);
    $dictionary[$new_key] = $contents; // add new
  }

  // create object instead of array
  if ($object == 'object') {
    $dictionary = (object) $dictionary;
  }

  return $dictionary;
}