Hi
JavaScript is the client side and PHP is the server side script language. The way to pass a JavaScript variable to PHP is through a request. 
Let’s take a look at a simple example:
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var arrayIDs = [18, 28 31, 38, 41]
$.ajax({
    url: 'main.php',
    type: 'post',
    data: {arrayIDs: arrayIDs},
    success: function(response){
        //do whatever.
    }
});
</script>
Your main.php file:
<?php
// Start the session
session_start();
$arrayIDs = $_POST['arrayIDs'];
$_SESSION["arrayIDs"] = arrayIDs;
?>
You should do sth like this and considet the session is a server side variable so you need to do it
other solution
If you want to dont use jquery you can do sth like this:
<script>
var arrayIDs = [18, 28 31, 38, 41]
var xhr = new XMLHttpRequest();
xhr.open('POST', 'main.php');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
    if (xhr.status === 200) {
        console.log(JSON.parse(xhr.responseText));
    }
};
xhr.send(JSON.stringify({
    arrayIDs: arrayIDs
}));
</script>
other solution
<script>
var arrayIDs = [18, 28 31, 38, 41]
fetch('main.php', {
  method: "POST",
  body: JSON.stringify(arrayIDs),
  headers: {"Content-type": "application/json; charset=UTF-8"}
})
.then(response => response.json()) 
.then(json => console.log(json));
.catch(err => console.log(err));
</script>