PHP递归数据

PHP Recursive data

美好的一天, 我希望有人可以帮助我...

我在一个系统上工作,你可以在其中捕获和加载测试代码,一些测试代码可以是组代码,组代码也可以包含组代码。我需要能够加载所有代码来自组和子组。

[Test Code] [isGroup] [groupCodes]
TEST         Yes       Code1,Code2,Code3
Code1        No
Code2        Yes       Code4,Code5
Code3        No
Code4        No
Code5        No

我一直在尝试多种选择,其中一些已经陷入无限循环。

这是我目前正在使用的代码,但这并没有得到子组

function get_group_test_codes($mysqli, $testcode){
    $test_codes = array();
    $test_data = get_test_by_code($mysqli, $testcode);

    if(empty($test_data)){
        return $test_codes;
    }

    if($test_data["isgroup"]==1){
        $group_codes = explode(",",$test_data["groupcodes"]);
        foreach($group_codes as $group_code){
            $test_codes[] = $group_code;
        }
    } else {
        $test_codes[] = $testcode;    
    }
    return $test_codes;
}

function get_test_by_code($mysqli, $testcode){
    $data = array();
    $sql = "SELECT * FROM `testtypes` where `testcode` = '{$testcode}' limit 1";
    $result = $mysqli->query($sql);
    while($row = $result->fetch_assoc()){
        $data = $row;
    }
    $result->free();
    return $data;
}

预期结果应该如下

array('Code1', 'Code3', 'Code4', 'Code5');

Code2 被排除在外,因为它是一个组代码,应该只加载它的组代码

要求:

  • 给定存储在 list of entries 中的 'TEST' 代码条目,其中可能包含 'codes'...
  • 的递归组
  • Return 所有 'TestCode' 的列表,这些 'TestCode' 是 'group' 的成员,但它们本身不是 'group'.

根据 OP,代码存储在数据库中。但是,'testCode' 一次读取一个条目 'testcode'。为了让我的生活更轻松,我设置了一个模拟数据库的数组 table 并提供了一个函数 readTestCode($testCode) 可以轻松更改为读取数据库而不是数组。

正在处理:

给定一个 testCode:

  • if a 'group' then process each code in it and 'recurse around the list' as required.
  • 否则输出`testcode'。

代码:

/** ------------------------------------------------------------------------
 * Given a TestCode - chase it down all the paths...
 *
 * @param string $testCode
 * @param array  $outCodes -- cummulative arrat - be reference
 */
function processOneTestCode($testCode, &$outCodes)
{
    $curCode = readTestCode($testCode); // get the details for this code
    if ($curCode['isGroup']) { // process all the records in the group
       foreach ($curCode['groupCode'] as $newCode) {
          processOneTestCode($newCode, $outCodes);
       }
    }
    else {
        $outCodes[] = $testCode; // add it to the output
    }
}

阅读 testCode details

将组展开为 testcode 的列表以便于处理...

/** -------------------------------------------------------------------------
 * This will be reading  one row of the database by TestCode
 * This version emulates the database read - accurately I expect! ;)
 *
 * @global array $dbTestCodes
 * @param string $testCode
 *
 * @return array (isGroup => bool, groupCode = array())
 */
function readTestCode($testCode)
{
    global $dbTestCodes; // lose this when using a database.

    $details = array();

    // put the database read here...

    if (isset($dbTestCodes[$testCode])) {
        $details = $dbTestCodes[$testCode];

        if ($details['isGroup']) { // explode the codes so we can  process them easily
            $codesList = explode(',', $details['groupCode']);
            $details['groupCode'] = $codesList;
        }
    }

    return $details;
}

运行它:

$outArray = array();  // output in here

/**
 *  process the one TestCode...
 */
processOneTestCode('TEST', $outArray);

/**
 *  the processOneTestCode function doesn't do any checks for duplicates...
 *
 *  So we need to remove duplicates and sort it so we can check it easily
 */
$outArray = array_unique($outArray);
sort($outArray);

// show it
echo '<pre>';
print_r($outArray);
echo '</pre>';
exit;

测试数据

/**
 * This is exactly how the database looks when you read it by `testCode`.
 */
$dbTestCodes = array(
    'TEST' => array('isGroup' => 1, 'groupCode' => 'CodeC,Code1,CodeB,Code2,Code3'),

    'Code1' => array('isGroup' => 0, 'groupCode' => ''),
    'Code2' => array('isGroup' => 1, 'groupCode' => 'Code4,Code5'),
    'Code3' => array('isGroup' => 0, 'groupCode' => ''),
    'Code4' => array('isGroup' => 0, 'groupCode' => ''),
    'Code5' => array('isGroup' => 0, 'groupCode' => ''),
    'CodeA' => array('isGroup' => 0, 'groupCode' => ''),
    'CodeB' => array('isGroup' => 1, 'groupCode' => 'CodeA,CodeD,Code2'),
    'CodeC' => array('isGroup' => 0, 'groupCode' => ''),
    'CodeD' => array('isGroup' => 0, 'groupCode' => ''),
);

输出:

Array
(
    [0] => Code1
    [1] => Code3
    [2] => Code4
    [3] => Code5
    [4] => CodeA
    [5] => CodeC
    [6] => CodeD
)