$prepared = [];
$max = 0;
foreach ($assoc as $columnName => $value$number) {
$assoc[$columnName]$rawLength = strlen($columnName . $number);
$max = max($max, $rawLength);
$prepared[htmlentities($columnName)] = [
'asDecimal' => (float) str_replace(',', '.', $value$number), // declared as first element for sorting
'value''length' => $value$rawLength,
'combinedLength''value' => strlen($columnName . $value)$number,
];
$max = max($max, $assoc[$columnName]['combinedLength']);
}
asort($assoc$prepared);
$min = 5;
foreach ($assoc$prepared as $columnName$encodedName => $value$subarray) {
$spaces$prepared[$encodedName]['glue'] = str_repeat(' ', $max - $value['combinedLength']$subarray['length'] + $min);
$assoc[$columnName]unset($prepared[$encodedName]['asDecimal'], =$prepared[$encodedName]['length']);
}
At this point you have this array data/structure:
array $columnName(
. $spaces'ALLIANZ' .=> $value['value'];
} array (
'value' => '86,96',
'glue' => ' ',
),
'AIG' =>
array (
'value' => '88,32',
'glue' => ' ',
),
'LLOYDS' =>
array (
'value' => '118,11',
'glue' => ' ',
),
)
Here is a (PHP DemoPHP Demo). Then return your json:
echo json_encode($assoc$prepare)
In your javascript, you need to iterate the associative structure. append()
is okay to use, but generating html tags from arguments via an object (there are no indexes to access values byjQuery('<option>', {...attributs&values})
) is going to automatically apply html encoding as a security measure. Instead, create <option>
tags as strings and append them as children into the <select>
tag.
dataType: "json",
success: function(data) {
data = {...{"":"Επιλέξτε τύπο πλωτού", ...data}}; // prepend your static property to the incoming object
jQuery.each(data, function (columnNameencodedName, twoColumnssubdata) {
jQuery('#ff_elem9589').append(jQuery('<option>',
{ '<option value="' + encodedName + '">'
value:+ columnName,encodedName + subdata.glue + subdata.value
text:+ twoColumns'</option>'
}));
});
}
Since <option>
tags cannot contain html tags (in a valid HTML document as far as I know), you can only try to hack the display by applying font-family: monospace
styling upon the option tags. This will ensure that all character have the same width -- this gives the effect of the split text being stuck to the left and right of each visible option.
Ultimately, I have to say that this probably a little too unsavoury of a hack for my liking. Unless you are fully committed to this approach, I recommend a re-think. Perhaps consider a set of radio buttons with the values; you might add a collapse/expand effect if you need to minimise the footprint on the webpage.