PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function unique_multidim_array($array, $key) { $temp_array = array(); $i = 0; $key_array = array(); foreach($array as $val) { if (!in_array($val[$key], $key_array)) { $key_array[$i] = $val[$key]; $temp_array[$i] = $val; } $i++; } return $temp_array; } $rows = [ 0 => [ 0 => "Дата", 1 => "Заявка", ], 1 => [ "data" => "2022-06-21 14:36:31", "id_question" => "10", ], 2 => [ "data" => "2022-06-18 13:30:32", "id_question" => "8", ], 3 => [ "data" => "2022-06-18 10:27:47", "id_question" => "8", ] ]; echo '<pre>' . print_r($rows, 1) . '</pre>'; $arr = [ 0 => [ "id" => 10, "name" => "Это десятый", ], 1 => [ "id" => 8, "name" => "Это восьмой", ] ]; echo '<pre>' . print_r($arr, 1) . '</pre>'; $test = array_map(function ($item) use ($arr, &$rows) { if (isset($item['id_question'])) { return array_merge($item, ['name' => $arr[array_search($item['id_question'], array_column($arr, 'id'))]['name']]); } else return $item; }, $rows); echo '<pre>' . print_r(unique_multidim_array($test, 'id_question'), 1) . '</pre>';
Show:  
Copy Clear