How to insert array to array PHP
Solution 1:
You don't need "array_combine()", "array_slice()" or similar. Just iterate through the data and create a new one:
$a = [2.2, 1.1, 3.9];
$b = ["B", "C", "F"];
$new = [];
foreach ($a as $index => $value) {
$new[] = [
$b[$index], // Fetch the value with the same index from the other array
$value
];
}
Demo: https://3v4l.org/oseSV
Solution 2:
You can use array_map to iterate over two arrays simultaneously and produce a new array:
$distance = array(3) {
[0]=>
float(2.2)
[1]=>
float(1.1)
[2]=>
float(3.9)
}
$getID = array(3) {
[0]=>
string(1) "B"
[1]=>
string(1) "C"
[2]=>
string(1) "F"
}
$output = array_map(function ($d, $id) {
return [$id, $d];
}, $distance, $getID);
Note that this code assumes that both arrays have the same length.
Offtopic:
A little advice about your code: always name your variable with something that allows you to know what's inside it, using correct plural too:
Your $distance variable contains an array off distances, so it nsme should be in plural.
Your $getID is not a function, so it should not be called get, but just $Ids instead.