Сама функция:
function mergeAs($key, $arr, $to) {
if (count($arr) !== count($to)) {
throw new \InvalidArgumentException ("Arrays must have the same size");
}
$merged = [];
foreach ($to as $index=>$element) {
$element[$key] = $arr[$index];
$merged[] = $element;
}
return $merged;
}
Использовать так:
$orderIds = ["7", "4", "3", "5", "6", "1"];
$files = [
[
'name' => "Screenshot from 2019-01-10 15-04-58.png",
'type' => "image/png",
'tmp_name' => "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a70d8",
'error' => 0,
'size' => 249784
],
[
'name' => "Screenshot from 2019-01-09 16-29-26.png",
'type' => "image/png",
'tmp_name' => "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a7179",
'error' => 0,
'size' => 440472
],
[
'name' => "Screenshot from 2019-01-07 18-10-31.png",
'type' => "image/png",
'tmp_name' => "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a71cb",
'error' => 0,
'size' => 175614
],
[
'name' => "par-de-ruedas-dentadas-dos-tamanos_318-44594.jpg",
'type' => "image/jpeg",
'tmp_name' => "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a721c",
'error' => 0,
'size' => 26891
],
[
'name' => "300614.png",
'type' => "image/png",
'tmp_name' => "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a7264",
'error' => 0,
'size' => 11784
],
[
'name' => "0.jpeg",
'type' => "image/jpeg",
'tmp_name' => "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a72ac",
'error' => 0,
'size' => 291469
]
];
$files = mergeAs("order", $orderIds, $files);
var_dump($files);
/* Result */
/*
array(6) {
[0]=>
array(6) {
["name"]=>
string(39) "Screenshot from 2019-01-10 15-04-58.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(107) "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a70d8"
["error"]=>
int(0)
["size"]=>
int(249784)
["order"]=>
string(1) "7"
}
[1]=>
array(6) {
["name"]=>
string(39) "Screenshot from 2019-01-09 16-29-26.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(107) "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a7179"
["error"]=>
int(0)
["size"]=>
int(440472)
["order"]=>
string(1) "4"
}
[2]=>
array(6) {
["name"]=>
string(39) "Screenshot from 2019-01-07 18-10-31.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(107) "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a71cb"
["error"]=>
int(0)
["size"]=>
int(175614)
["order"]=>
string(1) "3"
}
[3]=>
array(6) {
["name"]=>
string(48) "par-de-ruedas-dentadas-dos-tamanos_318-44594.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(107) "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a721c"
["error"]=>
int(0)
["size"]=>
int(26891)
["order"]=>
string(1) "5"
}
[4]=>
array(6) {
["name"]=>
string(10) "300614.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(107) "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a7264"
["error"]=>
int(0)
["size"]=>
int(11784)
["order"]=>
string(1) "6"
}
[5]=>
array(6) {
["name"]=>
string(6) "0.jpeg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(107) "/var/www/product/public/website/content/tmp/apartment_images_1927b69a6e00d870384ad6877cc50ece_5c3854e9a72ac"
["error"]=>
int(0)
["size"]=>
int(291469)
["order"]=>
string(1) "1"
}
}
*/