Select 9 elements from 3 arrays in PHP -
as part of autocomplete ajax script writing, pull total of 9 results 3 sets of results.
each result type has array holds maximum of 9 results type. there total of 3 result type arrays follows:
$places; $venues; $types;
the autosuggest should include maximum of 9 results of type. so, if take lenght of each array follows:
$places = 9; $venues = 9; $types = 9;
the result array should include 3 results $places
, 3 results $venues
, 3 results $types
.
however, if counts instead this:
$places = 1; $venues = 8; $types = 1;
the result array should included 1 result $places
, 7 results $venues
, 1 result $types
.
i can't life of me figure out efficient logic handle this. tried writing each instance in if() else if()
block, doesn't seem efficient. example:
$numplaces = count($places); $numvenues = count($venues); $numtypes = count($types); $total = $numplaces + $numvenues + $numtypes; $results = array(); if( $total <= $maxresults ) { foreach( return_x_items( $places, min(3, $numplaces) ) $item ) { $results[] = $item; } foreach( return_x_items( $venues, min(3, $numvenues) ) $item ) { $results[] = $item; } foreach( return_x_items( $types , min(3, $numtypes ) ) $item ) { $results[] = $item; } } else { $remaining = 9; if( $numplaces > $maxoftype && $numvenues > $maxoftype && $numtypes > $maxoftype ) { foreach( return_x_items( $places, 3 ) $item ) { $results[] = $item; } foreach( return_x_items( $venues, 3 ) $item ) { $results[] = $item; } foreach( return_x_items( $types , 3 ) $item ) { $results[] = $item; } } elseif( $numplaces > $maxoftype && $numvenues > $maxoftype && $numtypes < $maxoftype ) { foreach( return_x_items( $places, 3 ) $item ) { $results[] = $item; } foreach( return_x_items( $venues, ($maxresults - (3 + $numtypes) ) ) $item ) { $results[] = $item; } foreach( return_x_items( $types , $numtypes ) $item ) { $results[] = $item; } } elseif ( $numplaces > $maxoftype && $numvenues > $maxoftype && $numtypes < $maxoftype ) { } }
can suggest method achieve i'm trying do?
very done didn't have time, logic might able you, i'd approach this:
// result types $places = ['place']; $venues = ['venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue',]; $type = ['type']; // place them in common array $result_types = [ $places,$venues,$type ]; // sort smallest largest usort($result_types, 'sort_by_size'); function sort_by_size($a, $b){ return count($a) - count($b); } $result_count = 9; // want have max total of 9 results $return = []; ($i = 0; $i < $result_count; $i++) { // regardless of number of each result types, loop through 9 times foreach ($result_types &$types) { // loop through each result type in order specified earlier foreach ($types $key => $value) { // loop through type within each $return[] = $value; // take first value unset($types[$key]); // remove don't use again if (count($return) < $result_count) { break; // got result, break out , proceed next result type } else { break(3); // we've reached limit, break out of } } } }
so in case, should have 1 place, 1 type, , 7 venues
print_r($return); array ( [0] => place [1] => type [2] => venue [3] => venue [4] => venue [5] => venue [6] => venue [7] => venue [8] => venue )
if tried this:
$places = ['place','place','place','place']; $venues = ['venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue',]; $type = ['type'];
we should 4 places, 4 venues, 1 type? do
array ( [0] => type [1] => place [2] => venue [3] => place [4] => venue [5] => place [6] => venue [7] => place [8] => venue )
and this:
$places = ['place','place','place','place']; $venues = ['venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue','venue',]; $type = ['type','type','type','type'];
we should have 3 of each, do:
array ( [0] => place [1] => type [2] => venue [3] => place [4] => type [5] => venue [6] => place [7] => type [8] => venue )
done way, it'll try have equal amount of each result type feed it. when can't, it'll take have more fill until reaches specified max amount.
Comments
Post a Comment