如何以更好的方式编写下面的代码以保持无限循环?

How can I write below code in better way to keep looping endlessly?

这是我第一次使用 Whosebug,所以如果我的问题不清楚,请多多包涵!我试图从 tbl属性 中获取所有行并循环遍历数组以将其转换为关联数组。获取 属性 后,我将检查 属性 是否属于页面或对象。如果页面的 属性 如果我们在 propertiesArray 中添加相同的内容。如果 属性 是对象,我将检查该对象是属于页面还是属于另一个父对象的子对象。下面的代码适用于 3 个级别(意思是父 > 子 > 子 > 子)。但是,如果对象是另一个父对象,我需要改进它以连续循环。需要建议我如何改进它。下面的代码位于服务器端的 PHP 中。我希望将其带到客户端。 Angular JS 中的任何方式。预先感谢所有选择提供帮助的人!

$DBObj->querySelect("*","tbl属性","pgnid = $pageNID and propisactive = 1");

if($DBObj->fetchNumOfRows() >= 1){
    $queryRecords = $DBObj->fetchAllRecordsArray();
    $rows = $DBObj->fetchNumOfRows();
    
    for($i = 0; $i < $rows; $i++){
        //Properties of Objects are Loaded to Object Properties Array
        
        switch ($queryRecords[$i]['propof']){
            case "page":{
                if(array_key_exists($queryRecords[$i]['propofid'],$propertiesArray) != 1)
                    $propertiesArray[$queryRecords[$i]['propofid']] = [];
                array_push($propertiesArray[$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));                                        
                break;
            }
                
            case "object":{
                $DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecords[$i]['propofnid']);
                $queryRecord = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
                
                if($queryRecord != "")
                    switch($queryRecord['objof']){
                        case "object":{
                            $DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord['objofnid']);
                            $queryRecord1 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";

                            if($queryRecord1 != "")
                                switch($queryRecord1['objof']){
                                    case "object":{
                                        $DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord1['objofnid']);
                                        $queryRecord2 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
                                        
                                        if($queryRecord2 != "")
                                            switch($queryRecord2['objof']){
                                                case "object":{
                                                    if(array_key_exists($queryRecord2['objofid'].$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid'],$propertiesArray) != 1)
                                                        $propertiesArray[$queryRecord2['objofid'].$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']] = [];
                                                    array_push($propertiesArray[$queryRecord2['objofid'].$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
                                                    break;
                                                }
                                                    
                                                case "page":{
                                                    if(array_key_exists($queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid'],$propertiesArray) != 1)
                                                        $propertiesArray[$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']] = [];
                                                    array_push($propertiesArray[$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto'])); 
                                                    break;
                                                }
                                            }
                                        break;
                                    }

                                    case "page":{
                                        if(array_key_exists($queryRecord['objofid'].$queryRecords[$i]['propofid'],$propertiesArray) != 1)
                                            $propertiesArray[$queryRecord['objofid'].$queryRecords[$i]['propofid']] = [];
                                        array_push($propertiesArray[$queryRecord['objofid'].$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
                                        break;
                                    }
                                }
                            break;
                        }

                        case "page":{
                            if(array_key_exists($queryRecords[$i]['propofid'],$propertiesArray) != 1)
                                $propertiesArray[$queryRecords[$i]['propofid']] = [];
                            array_push($propertiesArray[$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));                            
                            break;
                        }
                    }
                break;
            }
                
            case "element":{                    
                $DBObj->querySelect("*","tblelement","pgnid = $pageNID and elisactive = 1 and elnid = ".$queryRecords[$i]['propofnid']);
                $queryRecord = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";  
                
                if($queryRecord != ""){
                    $DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord['elofnid']);
                    $queryRecord1 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
                
                    if($queryRecord1 != "")
                        switch($queryRecord1['objof']){
                            case "object":{
                                $DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord1['objofnid']);
                                $queryRecord2 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
                                
                                if($queryRecord2 != ""){
                                    switch($queryRecord2['objof']){
                                        case "object":{
                                            $DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord2['objofnid']);
                                            $queryRecord3 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
                                            
                                            if($queryRecord3 != ""){
                                                switch($queryRecord3['objof']){
                                                    case "object":{
                                                        
                                                        break;
                                                    }
                                                        
                                                    case "page":{
                                                        if(array_key_exists($queryRecord3['objid'].$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid'],$propertiesArray) != 1)
                                                            $propertiesArray[$queryRecord3['objid'].$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']] = [];
                                                        array_push($propertiesArray[$queryRecord3['objid'].$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
                                                        break;
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                            
                                        case "page":{
                                            if(array_key_exists($queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid'],$propertiesArray) != 1)
                                                $propertiesArray[$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']] = [];
                                            array_push($propertiesArray[$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
                                            break;
                                        }
                                    }                                    
                                }                                                                
                                break;
                            }

                            case "page":{
                                if(array_key_exists($queryRecord1['objid'].$queryRecord['elid'],$propertiesArray) != 1)
                                    $propertiesArray[$queryRecord1['objid'].$queryRecord['elid']] = [];
                                array_push($propertiesArray[$queryRecord1['objid'].$queryRecord['elid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));                            
                                break;
                            }
                        }
                }
                break;
            }
        }
    }
} 

要递归迭代对象,您需要... 递归

创建带有条件的独立函数。

  • 如果当前项目属于页面 - 推送到数组并且 return
  • 如果当前项属于对象 - 获取子元素并再次重复此检查。

这是伪代码,根据需要调整:

if($DBObj->fetchNumOfRows() >= 1){
    $queryRecords = $DBObj->fetchAllRecordsArray();
    $rows = $DBObj->fetchNumOfRows();
    
    for($i = 0; $i < $rows; $i++){
        recursiveAction($queryRecords[$i]);
    }
}

function recursiveAction(&$item, $propertiesArray) {
    switch ($item['propof']) {
        case 'page':
            // No need for that "if" check
            $propertiesArray[$item['propofid']][] = [$item['propid'] => $item['propvalue'],'proplinkedto' => $item['proplinkedto']];

            break;
        case 'object':
            $DBObj->querySelect(...);
            $queryRecord = $DBObj->fetchNumOfRows() == 1 ? $DBObj->fetchRecordArray() : "";

            if (!empty($queryRecord)) {
                // Here we convert common part of "object" into recursion
                recursiveAction($queryRecord);
            }

            break;
      case 'element':
            ...
    }
}