I’m confused as to why this code:
$mapped_class = ( $mapped_field_index = array_search( $field_name, $automapped_header ) !== false ) ? " mapped mapped_to-" . $mapped_field_index : "";
… consistently returns 1 as the $mapped_field_index (when applicable)
Whereas this, expanded code:
$mapped_field_index = array_search( $field_name, $automapped_header );
$mapped_class = $mapped_field_index !== false ? " mapped mapped_to-" . $mapped_field_index : "";
… displays the correct search index as the $mapped_field_index.
I thought that in PHP an assignment within an IF context is evaluated as an expression as well and returns the assigned value. This seems to hold true in both these examples as the $mapped_class is blank in situations where the array_search() yields no results.
But I would have expected $mapped_field_index to contain the correct array_search() index, in both cases, as opposed to just 1 in the ternary form (which seems to indicate TRUE rather than an actual index).
Does the ternary operator have a contributing effect here?
Comparison operators have a higher precedence than assignment. See http://php.net/manual/en/language.operators.precedence.php
(your !== clause is being grouped before your = clause)
Try adding parentheses like this (not 100% sure it will work, but it may)