PHP生成具有多维数组变体的列表

时间:2022-04-29 01:33:15

I am really stuck on trying to create php queries to generate report from multidimensional array.

我真的坚持尝试创建PHP查询以从多维数组生成报告。

  1. Generate random names based on group number selected
  2. 根据所选的组编号生成随机名称

  3. Print 3 random names first selected group AND print 2 random names other selected group
  4. 打印3个随机名称首先选择组并打印2个随机名称其他所选组

Data Example:

 $group1 = [
            'Type 1' = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
            'Type 2' = ['Name 5', 'Name 6', 'Name 7', 'Name 8']
            'Type 3' = ['Name 9', 'Name 10', 'Name 11', 'Name 12']
             ]

$group2 = [
            'Type 1' = ['Name 13', 'Name 14', 'Name 15', 'Name 16']
            'Type 2' = ['Name 17', 'Name 18', 'Name 19', 'Name 20']
            'Type 3' = ['Name 21', 'Name 22', 'Name 23', 'Name 24']
             ]

 $group3 = [
            'Type 1' = ['Name 25', 'Name 26', 'Name 27', 'Name 28']
            'Type 2' = ['Name 29', 'Name 30', 'Name 31', 'Name 32']
            'Type 3' = ['Name 33', 'Name 34', 'Name 35', 'Name 36']
             ]
…

PHP output Example :

PHP输出示例:

Group Selected:  1 AND 3

Type 1: Name 1, Name 3, Name 2, Name 26, Name 28
Type 2: Name 6, Name 5, Name 7, Name 30, Name 31
Type 3: Name 9, Name 10, Name 11, Name 35, Name 36

I really appreciated your response.

我非常感谢你的回复。

2 个解决方案

#1


$group1 = array(
            'Type 1' => array('Name 1', 'Name 2', 'Name 3', 'Name 4'),
            'Type 2' => array('Name 5', 'Name 6', 'Name 7', 'Name 8'),
            'Type 3' => array('Name 9', 'Name 10', 'Name 11', 'Name 12')
             );

$group2 = array(
            'Type 1' => array('Name 13', 'Name 14', 'Name 15', 'Name 16'),
            'Type 2' => array('Name 17', 'Name 18', 'Name 19', 'Name 20'),
            'Type 3' => array('Name 21', 'Name 22', 'Name 23', 'Name 24')
             );

$group3 = array(
            'Type 1' => array('Name 25', 'Name 26', 'Name 27', 'Name 28'),
            'Type 2' => array('Name 29', 'Name 30', 'Name 31', 'Name 32'),
            'Type 3' => array('Name 33', 'Name 34', 'Name 35', 'Name 36')
             );

$selected = array(1,3);

foreach (array(1,2,3) as $idx) {
    $sel1 = ${'group' . reset($selected)}['Type ' . $idx];
    $sel2 = ${'group' . end($selected)}['Type ' . $idx];
    $found1 = array_intersect_key($sel1, array_flip(array_rand($sel1, 3)));
    $found2 = array_intersect_key($sel2, array_flip(array_rand($sel2, 2)));
    echo "Type $idx: " . implode (", ", $found1) . ', ' .  implode (",", $found2);
    echo "<br>";
}

#2


If i correctly understood

如果我正确理解

$group1 = array(
            'Type 1' => array('Name 1', 'Name 2', 'Name 3', 'Name 4'),
            'Type 2' => array('Name 5', 'Name 6', 'Name 7', 'Name 8'),
            'Type 3' => array('Name 9', 'Name 10', 'Name 11', 'Name 12')
             );

$group2 = array(
            'Type 1' => array('Name 13', 'Name 14', 'Name 15', 'Name 16'),
            'Type 2' => array('Name 17', 'Name 18', 'Name 19', 'Name 20'),
            'Type 3' => array('Name 21', 'Name 22', 'Name 23', 'Name 24')
             );

$group3 = array(
            'Type 1' => array('Name 25', 'Name 26', 'Name 27', 'Name 28'),
            'Type 2' => array('Name 29', 'Name 30', 'Name 31', 'Name 32'),
            'Type 3' => array('Name 33', 'Name 34', 'Name 35', 'Name 36')
             );
// Array of selected groups          
$selected_groups = array(1,3);
// Making names of selected group arrays
print_r(make_array(${'group'.$selected_groups[0]}, ${'group'.$selected_groups[1]}));

function make_array($ar1, $ar2) {
// 'Type 1', 'Type 2', 'Type 3' 
    foreach (array_keys($ar1) as $key)
// Collect in $res what we want - 3 names from 1st array and 2 from 2nd
        $res[$key] = array_merge(array_rand(array_flip($ar1[$key]), 3), array_rand(array_flip($ar2[$key]), 2));
    return $res;
}

DEMO

#1


$group1 = array(
            'Type 1' => array('Name 1', 'Name 2', 'Name 3', 'Name 4'),
            'Type 2' => array('Name 5', 'Name 6', 'Name 7', 'Name 8'),
            'Type 3' => array('Name 9', 'Name 10', 'Name 11', 'Name 12')
             );

$group2 = array(
            'Type 1' => array('Name 13', 'Name 14', 'Name 15', 'Name 16'),
            'Type 2' => array('Name 17', 'Name 18', 'Name 19', 'Name 20'),
            'Type 3' => array('Name 21', 'Name 22', 'Name 23', 'Name 24')
             );

$group3 = array(
            'Type 1' => array('Name 25', 'Name 26', 'Name 27', 'Name 28'),
            'Type 2' => array('Name 29', 'Name 30', 'Name 31', 'Name 32'),
            'Type 3' => array('Name 33', 'Name 34', 'Name 35', 'Name 36')
             );

$selected = array(1,3);

foreach (array(1,2,3) as $idx) {
    $sel1 = ${'group' . reset($selected)}['Type ' . $idx];
    $sel2 = ${'group' . end($selected)}['Type ' . $idx];
    $found1 = array_intersect_key($sel1, array_flip(array_rand($sel1, 3)));
    $found2 = array_intersect_key($sel2, array_flip(array_rand($sel2, 2)));
    echo "Type $idx: " . implode (", ", $found1) . ', ' .  implode (",", $found2);
    echo "<br>";
}

#2


If i correctly understood

如果我正确理解

$group1 = array(
            'Type 1' => array('Name 1', 'Name 2', 'Name 3', 'Name 4'),
            'Type 2' => array('Name 5', 'Name 6', 'Name 7', 'Name 8'),
            'Type 3' => array('Name 9', 'Name 10', 'Name 11', 'Name 12')
             );

$group2 = array(
            'Type 1' => array('Name 13', 'Name 14', 'Name 15', 'Name 16'),
            'Type 2' => array('Name 17', 'Name 18', 'Name 19', 'Name 20'),
            'Type 3' => array('Name 21', 'Name 22', 'Name 23', 'Name 24')
             );

$group3 = array(
            'Type 1' => array('Name 25', 'Name 26', 'Name 27', 'Name 28'),
            'Type 2' => array('Name 29', 'Name 30', 'Name 31', 'Name 32'),
            'Type 3' => array('Name 33', 'Name 34', 'Name 35', 'Name 36')
             );
// Array of selected groups          
$selected_groups = array(1,3);
// Making names of selected group arrays
print_r(make_array(${'group'.$selected_groups[0]}, ${'group'.$selected_groups[1]}));

function make_array($ar1, $ar2) {
// 'Type 1', 'Type 2', 'Type 3' 
    foreach (array_keys($ar1) as $key)
// Collect in $res what we want - 3 names from 1st array and 2 from 2nd
        $res[$key] = array_merge(array_rand(array_flip($ar1[$key]), 3), array_rand(array_flip($ar2[$key]), 2));
    return $res;
}

DEMO