How to manipulate some fields using Views PHP? I'm trying to manipulate some fields on Views with Views PHP.

I have a (term) field for author(s). It can have one author or more.

Normally it looks like as that:

Book Title | Author

My First Book | Name1 Surname1, Name2 Middle2 Surname2

My Second Book | Name3 Surname3

etc.



What I want to do is to print names as that:

Book Title | Author

My First Book | Surname1, Name1 - Surname2, Name2 Middle2 

My Second Book | Surname3, Name3 



In order to do that, I used Global: PHP field with Content: Authors.

Value code inside Global: PHP is that:

$names = $row-&gt;field_authors;
foreach ($names as $name) {
  $parts = explode(' ', $name);
  if (count($parts) == 1) {
    $wanted = print($name);
    continue;
  }
  $last = array_pop($parts);
  $wanted = print ($last  . implode(' ', $parts));
}
return $wanted;




Output code is:

&lt;?php print $value; ?&gt;




But it doesn't return anything. I tried some other similar code but nothing changed.

Is it about the Content: Authors field (multiple field settings etc.) or am I wrong about the code I used in the Global: PHP field?
You'll want to put that code into a view template. Go into your admin, where you can edit that View, click on Advanced, and then Theme: Information. That will give you suggestions on the file naming conventions for the row style output for your view.

But as for your PHP code, Drupal uses a lot of nested multi-dimensional arrays, so it's not as simple as $row-&gt;field_authors being an array of strings of author names (unless you're doing some preprocessing). If field_authors is a term reference, you'll need to load the taxonomy term in order to get the actual author name.

This should get you the $names array:

$names = [];
foreach($row-&gt;field_field_authors as $author_tid){
   $term = taxonomy_term_load($author_tid[0]['tid']);
   $names[] = $term-&gt;name;
}
(... the rest of your code...)