Getting a field value from a view I am new to Drupal8. In one of the preprocess functions in my customtheme.theme file, I need to get the value of a field from a view that I have already created. I searched the web and got:
 $view = $variables['view'];
    $view_result = $view-&gt;result;
    if (!empty($view_result)) {
      foreach ($view-&gt;result as $rid =&gt; $row) {
        foreach ($view-&gt;field as $fid =&gt; $field ) {
          $values[$rid][$fid] = $field-&gt;getValue($row);
        }
      }
    }
   $array[0] = $values[0]['nid'];

AND i got another one too :
$view = $variables['view'];
$values = [];
foreach ($view-&gt;result as $row_id =&gt; $row) {
  foreach ($view-&gt;field as $field_id =&gt; $field ) {
    $values[$row_id][$field_id] = $field-&gt;getValue($row);
  }
}


The first solution worked but I want an explanation of it please especially that I tried to print_r ($view_result) and that didn't work, so I managed to print its keys and got onyl one key which is 0. Then I got many weird keys when I did a foreach to print the keys of  $view_result[0].
I would be grateful if someone can explain to me the code and why did the getValue function take an argument ? I usually write it as getValue() only.
What you usually write as getValue() without arguments is a method of an entity FieldItemList.
$view-&gt;field returns field handler plugins configured in the FIELDS section of a View.
Views fields handler plugins are an abstraction layer for all kind of data not necessarily based on entity fields.
If based on entity fields then getValue($row) runs this code:
/core/modules/views/src/Plugin/views/field/EntityField.php:
 public function getValue(ResultRow $values, $field = NULL) {
    $entity = $this-&gt;getEntity($values);

    // Ensure the object is not NULL before attempting to translate it.
    if ($entity === NULL) {
      return NULL;
    }

    // Retrieve the translated object.
    $translated_entity = $this-&gt;getEntityFieldRenderer()-&gt;getEntityTranslation($entity, $values);

    // Some bundles might not have a specific field, in which case the entity
    // (potentially a fake one) doesn't have it either.
    /** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
    $field_item_list = isset($translated_entity-&gt;{$this-&gt;definition['field_name']}) ? $translated_entity-&gt;{$this-&gt;definition['field_name']} : NULL;

    if (!isset($field_item_list)) {
      // There isn't anything we can do without a valid field.
      return NULL;
    }

    $field_item_definition = $field_item_list-&gt;getFieldDefinition();

    $values = [];
    foreach ($field_item_list as $field_item) {
      /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
      if ($field) {
        $values[] = $field_item-&gt;$field;
      }
      // Find the value using the main property of the field. If no main
      // property is provided fall back to 'value'.
      elseif ($main_property_name = $field_item-&gt;mainPropertyName()) {
        $values[] = $field_item-&gt;{$main_property_name};
      }
      else {
        $values[] = $field_item-&gt;value;
      }
    }
    if ($field_item_definition-&gt;getFieldStorageDefinition()-&gt;getCardinality() == 1) {
      return reset($values);
    }
    else {
      return $values;
    }
  }

You can see how Views API connects with Entity API, but when programming Views API code you should stay at this abstraction layer if possible.