단지 내장 함수를 사용하여 문제를 해결하는 간단한 방법이처럼은 보이지 않는다.
(적어도 논리적으로는) 작동해야합니다.
$search_terms = array('a', 'b', 'c', 'c', 'd', 'd');
$search_types = array(1, 2, 3, 4, 5, 5);
$terms = array_fill_keys($search_terms, array());
// Loop through them once and make an array of types for each term
foreach ($search_terms as $i => $term)
$terms[$term][] = $search_types[$i];
// Now we have $terms = array('a' => array(1),
// 'b' => array(2),
// 'c' => array(3, 4),
// 'd' => array(5, 5)
// );
// Now run through the array again and get rid of duplicates.
foreach ($terms as $i => $types)
$terms[$i] = array_unique($types);
편집 :
$search_terms = array('a', 'b', 'c', 'c', 'd', 'd');
$search_types = array(1, 2, 3, 4, 5, 5);
$terms = array_fill_keys($search_terms, array());
// Loop through them once and make an array of types for each term
foreach ($search_terms as $i => $term)
$terms[$term][$search_types[$i]] = 1;
// Now we have $terms = array('a' => array(1 => 1),
// 'b' => array(2 => 1),
// 'c' => array(3 => 1, 4 => 1),
// 'd' => array(5 => 1)
// );
당신이 두 개의 배열을 유지해야합니까 : 여기에 짧은 당신이 덜 예쁜 배열로 끝날 일이 아마도 더 효율적? –
예를들 수 있습니까? –
@Casey Hope : 아니요, 보존 할 필요가 없습니다 – babonk