sql >> Database >  >> RDS >> Mysql

Hoe een array opnieuw op te bouwen zonder herhalingen en andere limieten?

Dit is wat ik uiteindelijk bedacht dat werkte (na een mislukte poging om de query te bouwen die ik nodig had om hetzelfde te bereiken)...

De originele array $theresults bevat alle 60 vragen uit de 5 verschillende categorieën. Ik begin met het bouwen van een array van alle vraagcategorieën...

// Create array of all categories
$allcategories = array();
$this->db->select('category_id');
$this->db->where('template_id',$template_id);
$query_thecategories = $this->db->get('categories');
$number_thecategories = $query_thecategories->num_rows();
if ($number_thecategories>0) {
    foreach ($query_thecategories->result() as $row_thecategory) {
        $thecategory = 'cat_' . $row_thecategory->category_id;
        $$thecategory = '0';
        $allcategories[] = $row_thecategory->category_id;
    }
}

Dan gebruik ik de volgende functie om alle unieke combinaties van categorieën te trekken...

function array_search_by_key($array, $key, $value) {
    if(!is_array($array)) {
        return [];
    }
    $results = [];
    foreach($array as $element) {
        if(isset($element[$key]) && $element[$key] == $value) {
            $results[] = $element;
        }
    }
    return $results;
}

$uniquecombos = uniquecombos($allcategories, 2);

Ten slotte loop ik door elk van de combo's om vragen te stellen die overeenkomen met elke categorie in het paar en sla het resultaat op in een nieuwe array. (Ik herhaal het drie keer omdat elke categoriekoppeling drie keer zal worden gebruikt (10 combinaties van vraagparen x 3 lussen =60 vragen.) Ik verwijder ook elke vraag die ik trek uit de originele $theresults array om er zeker van te zijn dat er geen duplicaten zijn...

// Create an empty array to capture the paired questions
$pairs = array();

// Loop through unique combos array 3 times to build pairings
for($combos = 1; $combos <= 3; $combos++) {
    foreach ($uniquecombos as $theset) {
        // Get two categories in pair
        $firstcategory = $theset[0];
        $secondcategory = $theset[1];

        // Gather other arrays which matches each category
        $matchesfirst = array_search_by_key($theresults,'category_id',$firstcategory);
        shuffle($matchesfirst);
        $matchessecond = array_search_by_key($theresults,'category_id',$secondcategory);
        shuffle($matchessecond);

        // Get question from each new array & add; remove selected question from the original array
        $pairs[] = $matchesfirst[0];
        unset($theresults[$matchesfirst[0]['question_id']]);
        $pairs[] = $matchessecond[0];
        unset($theresults[$matchessecond[0]['question_id']]);
    }
}

Hopelijk helpt dit iemand anders!




  1. TypeError:kan methode 'query' van null niet aanroepen - bij het aanroepen van pg.connect met Heroku node.js

  2. Hoe de databaseverbinding op een python-webserver te behouden?

  3. Impact van uitvoeringsplan op ASYNC_NETWORK_IO Wachten - Deel 1

  4. Hoe Oracle Linux Automation Manager te installeren (ook bekend als "Oracle Ansible Tower")