@Lavrov95

Как правильно объединить массив?

объединить $a с $b добавив key

$a
array(6) {
  [1]=>
  string(1) "7"
  [2]=>
  string(1) "4"
  [3]=>
  string(1) "3"
  [4]=>
  string(1) "5"
  [5]=>
  string(1) "6"
  [6]=>
  string(1) "1"
}


$b
array(5) {
  ["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)
}
array(5) {
  ["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)
}
array(5) {
  ["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)
}
array(5) {
  ["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)
}
array(5) {
  ["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)
}
array(5) {
  ["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)
}


output

array(5) {
  ["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"]=>
  int(5)
}
  • Вопрос задан
  • 69 просмотров
Пригласить эксперта
Ответы на вопрос 2
Так?

Код
<?php

$a = [
	1,
	3,
	6
];

$b = [
	[
		'Simple String 0'
	],
	[
		'Simple String 1'
	],
	[
		'Simple String 2'
	],
	[
		'Simple String 3'
	],
	[
		'Simple String 4'
	],
	[
		'Simple String 5'
	],
	[
		'Simple String 6'
	],
	[
		'Simple String 7'
	],
	[
		'Simple String 8'
	]
];

$c = [];
foreach($b as $index => $item) if(in_array($index, $a)) $c[] = $item;

var_dump($c);
/* array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(15) "Simple String 1"
  }
  [1]=>
  array(1) {
    [0]=>
    string(15) "Simple String 3"
  }
  [2]=>
  array(1) {
    [0]=>
    string(15) "Simple String 6"
  }
} */
Ответ написан
Immortal_pony
@Immortal_pony
Сама функция:
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"
  }
} 
*/
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через TM ID
Похожие вопросы
Интерно Ростов-на-Дону
от 35 000 до 50 000 руб.
Ticketland.ru Москва
от 120 000 руб.
СиСорт Барнаул
от 40 000 до 115 000 руб.
22 янв. 2019, в 21:11
800 руб./в час
22 янв. 2019, в 20:53
6006 руб./за проект
22 янв. 2019, в 20:48
20000 руб./за проект