0

I am trying to use extra fields in K2. I do it all the time - but in this case, sometimes some fields have values, and others don't. So the key is not consistent.

Is there a way for me to refer to them by name or id instead of by key of the automatic array?

I tried this and it does not seem to work:

$streetaddress = $this->item->extra_fields->id[249];
Shaz
  • 2,647
  • 2
  • 35
  • 45
itamar
  • 3,837
  • 5
  • 35
  • 60

1 Answers1

1

Found this great script here for converting extra fields to use either id or label as key:

<?php 
 //convertArray to use ids as key
      $extrafieldsid = array();
      foreach($this->item->extra_fields as $itemid)
      {    
      $extrafieldsid[$itemid->id] = $itemid->value;
      }   
      ?>
       <?php 
 //convertArray to use labels as key
      $extrafieldslabels = array();
      foreach($this->item->extra_fields as $itemlabel)
      {    
      $extrafieldslabels[$itemlabel->id] = $itemlabel->name;
      }   
      ?>
itamar
  • 3,837
  • 5
  • 35
  • 60