My code takes the input submitted by $ _POST, via a checkbox array. I process the array and check the sent data if there is a particular string in it, which is set as my default. If I have a string, which is my default, I just go through implode and add ",", if my default string is missing in the data. I add and sort the array anew, with the first string being the default. I want to shorten the code because I can't make it shorter.
<?php
if(isset($_POST['submit'])) {
//for update
$site_id['locale'] = 'tr';
//new
$exampleArray = isset($_POST['lala']) ? $_POST['lala'] : '';
$example = null;
if(($key = array_search($site_id['locale'], $exampleArray, true)) !== false) {
unset($exampleArray[$key]);
$FirstString = array($site_id['locale']);
$exampleArray = array_diff($exampleArray, $FirstString);
usort($exampleArray);
$exampleArray = array_merge($FirstString, $exampleArray);
//print_r($exampleArray);
$myArray = array();
foreach ( $exampleArray as $key => $value ) {
$myArray[] = $value;
}
echo implode( ', ', $myArray );
} else {
$FirstString = array($site_id['locale']);
$exampleArray = array_diff($exampleArray, $FirstString);
usort($exampleArray);
$exampleArray = array_merge($FirstString, $exampleArray);
//print_r($exampleArray);
$myArray = array();
foreach ( $exampleArray as $key => $value ) {
$myArray[] = $value;
}
echo implode( ', ', $myArray );
}
}
?>
<form method="POST" action="">
<input type="checkbox" name="lala[]" value="bg" />
<input type="checkbox" name="lala[]" value="us" />
<input type="checkbox" name="lala[]" value="gr" />
<input type="submit" name="submit" />
</form>