Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

बिना दोहराव और अन्य सीमाओं के साथ एक सरणी का पुनर्निर्माण कैसे करें?

यहाँ मैं अंत में उस काम के साथ आया हूँ (उसी काम को पूरा करने के लिए आवश्यक क्वेरी बनाने के असफल प्रयास के बाद)...

मूल सरणी $theresults 5 विभिन्न श्रेणियों के सभी 60 प्रश्न शामिल हैं। मैं सभी प्रश्न श्रेणियों की एक सरणी बनाकर शुरू करता हूं...

// 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;
    }
}

फिर मैं श्रेणियों के सभी अद्वितीय संयोजनों को खींचने के लिए निम्न फ़ंक्शन का उपयोग करता हूं...

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);

अंत में, मैं जोड़ी में प्रत्येक श्रेणी से मेल खाने वाले प्रश्नों को खींचने के लिए प्रत्येक कॉम्बो के माध्यम से लूप करता हूं और परिणाम को एक नई सरणी में संग्रहीत करता हूं। (मैं इसे तीन बार लूप करता हूं क्योंकि प्रत्येक श्रेणी जोड़ी का तीन बार उपयोग किया जाएगा (प्रश्न जोड़े के 10 संयोजन x 3 लूप =60 प्रश्न।) मैं मूल $theresults से प्रत्येक प्रश्न को भी हटा देता हूं। यह सुनिश्चित करने के लिए कि कोई डुप्लीकेट नहीं हैं...

// 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']]);
    }
}

उम्मीद है कि यह किसी और की मदद करता है!




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. विंडोज 7 पर mysql सर्वर को पुनरारंभ करें

  2. कोड पिंग टाइम मीटर - क्या यह सच में सच है?

  3. निर्दिष्ट कुंजी बहुत लंबी थी; अधिकतम कुंजी लंबाई 1000 बाइट्स है

  4. mysql 3 टेबल में शामिल हों

  5. प्रयोक्ताओं को उपयोक्तानाम से user_group में मिलाना