简体   繁体   中英

Assign array value to a field

I am working with migration and I am migrating taxonomy terms that the document has been tagged with. The terms are in the document are separated by commas. so far I have managed to separate each term and place it into an array like so:

public function prepareRow($row) {
    $terms = explode(",", $row->np_tax_terms);
        foreach ($terms as $key => $value) {
          $terms[$key] = trim($value);
        }
        var_dump($terms);
        exit;
}

This gives me the following result when I dump it in the terminal:

array(2) {
  [0]=>
  string(7) "Smoking"
  [1]=>
  string(23) "Not Smoking"
}

Now I have two fields field_one and field_two and I want to place the value 0 of the array into field_one and value 1 into field_two eg

field_one=[0]$terms;

I know this isn't correct and I'm not sure how to do this part. Any suggestions on how to do this please?

If you are only looking to store the string value of the taxonomy term into a different field of a node, then the following code should do the trick:

$node->field_one['und'][0]['value'] = $terms[0];
$node->field_two['und'][0]['value'] = $terms[1];
node_save($node);

Note you will need to load the node first, if you need help with that, comment here and will update my answer.

You are asking specifically about ArrayList and HashMap, but I think to fully understand what is going on you have to understand the Collections framework. So an ArrayList implements the List interface and a HashMap implements the Map interface.

List:

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Map:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

So as other answers have discussed, the list interface (ArrayList) is an ordered collection of objects that you access using an index, much like an array (well in the case of ArrayList, as the name suggests, it is just an array in the background, but a lot of the details of dealing with the array are handled for you). You would use an ArrayList when you want to keep things in sorted order (the order they are added, or indeed the position within the list that you specify when you add the object).

A Map on the other hand takes one object and uses that as a key (index) to another object (the value). So lets say you have objects which have unique IDs, and you know you are going to want to access these objects by ID at some point, the Map will make this very easy on you (and quicker/more efficient). The HashMap implementation uses the hash value of the key object to locate where it is stored, so there is no guarentee of the order of the values anymore.

You might like to try:

list($field_one, $field_two) = prepareRow($row);

The list function maps entries in an array (in order) to the variables passed by reference.

This is a little fragile, but should work so long as you know you'll have at least two items in your prepareRow result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM