Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Làm thế nào để xây dựng lại một mảng không có lặp lại và các giới hạn khác?

Đây là những gì cuối cùng tôi nghĩ ra đã hoạt động (sau khi cố gắng tạo truy vấn không thành công mà tôi cần để hoàn thành điều tương tự) ...

Mảng ban đầu $theresults chứa tất cả 60 câu hỏi từ 5 danh mục khác nhau. Tôi bắt đầu bằng cách xây dựng một loạt tất cả các loại câu hỏi ...

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

Sau đó, tôi sử dụng hàm sau để kéo tất cả các tổ hợp danh mục duy nhất ...

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

Cuối cùng, tôi lặp qua từng tổ hợp để lấy các câu hỏi phù hợp với từng danh mục trong cặp và lưu trữ kết quả trong một mảng mới. (Tôi lặp lại nó ba lần vì mỗi cặp danh mục sẽ được sử dụng ba lần (10 tổ hợp các cặp câu hỏi x 3 vòng lặp =60 câu hỏi.) Tôi cũng xóa từng câu hỏi tôi lấy từ $theresults ban đầu mảng để đảm bảo không có bản sao ...

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

Hy vọng rằng điều này sẽ giúp ích cho người khác!




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Ví dụ DAYOFWEEK () - MySQL

  2. Mẫu so sánh với mysql giữa hai cột bảng

  3. mysql-php truy vấn danh sách để tạo bảng

  4. NullPointerException - Sự cố bị khóa cơ sở dữ liệu khi truy xuất dữ liệu từ cơ sở dữ liệu và lưu trữ vào một mảng

  5. Tải lại dữ liệu MySQL bên trong DIV bằng Ajax