Yngvarr

Generation

generate functionMon, 14 Nov 2022

новый массив по ключу из другого многомерного массива

<?php function filterByKey($array, $keyName) { $result = []; foreach ($array as $item) { if (isset($item[$keyName])) { $result[] = $item[$keyName]; } } return array_unique($result); } $array = [ ['product_id' => 'xxxx', 'name' => 'iphone'], ['product_id' => 'yyyy', 'name' => 'samsung'], ['product_id' => 'xxxx', 'name' => 'iphone'], ]; $result = filterByKey($array, 'product_id'); print_r($result);

Questions about programming?Chat with your personal AI assistant