PHP中的奇怪数组

发布于 2024-09-01 13:35:38 字数 3818 浏览 2 评论 0原文

这里我写了一个函数,它的一般用途是获取父根$depId下的depId数组。

我使用递归方法来获取数组:

public function getEmpsByDep($depId){
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId;
    $stmt=$this->db->query($query);     
    while(($row=$this->db->fetch_assoc($stmt))==true)
    {   
        if($this->hasChildNode($row['DEPID']))
        {
            $depId = $row['DEPID'];
            self::getEmpsByDep($depId);
        }
        else
        {
            $arr[]=$row['DEPID'];
        }
    }
    return ($arr);
}

这里有 hasChildNode 函数来检查指定的 $depId 是否有子部门:

public function hasChildNode($depId)
{
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId;
    $stmt=$this->db->query($query);
    $row=$this->db->fetch_assoc($stmt);
    if($row==false){
        return false;
    }else
        return true;
}

虽然我认为它应该返回 depid 的一维数组。但是当调用时:

$this->getEmpsByDep(0);

它在更改时返回一个奇怪的二维数组,如下所示getEmpsByDep 函数中的“return”到“var_dump”:

 array(4) {
      [0]=>
      string(2) "11"
      [1]=>
      string(2) "12"
      [2]=>
      string(2) "13"
      [3]=>
      string(2) "14"
    }
    array(3) {
      [0]=>
      string(2) "19"
      [1]=>
      string(2) "20"
      [2]=>
      string(2) "21"
    }
    array(3) {
      [0]=>
      string(2) "15"
      [1]=>
      string(2) "16"
      [2]=>
      string(2) "17"
    }
    array(8) {
      [0]=>
      string(1) "2"
      [1]=>
      string(1) "4"
      [2]=>
      string(1) "5"
      [3]=>
      string(1) "6"
      [4]=>
      string(1) "7"
      [5]=>
      string(1) "8"
      [6]=>
      string(1) "9"
      [7]=>
      string(2) "10"
    }

这里是表结构和数据示例:

$query[]="create table ".$sqltblpre."department(
     depId number(10) not null primary key,
     depName varchar2(50) not null,
     id_parent number(10)
)";

//department(部门和岗位)
$index=1;
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'院部',0)";  //1
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'政治部',0)"; //2
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'医务部',0)"; //3
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'护理部',0)"; //4
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'经济部',0)";  //5
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'信息科',0)";  //6
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'医学工程科',0)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'门诊系统',0)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'内科系统',0)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'外科系统',0)";

$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'院长',1)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'政委',1)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'副院长',1)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'秘书',1)";

$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'主任',3)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'副主任',3)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'助理员',3)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'训练队',3)"; //18

$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'队长',18)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'助理员',18)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'队员',18)";

所以总而言之,我怎样才能得到这个函数的正确代码的一维数组?

here i wrote a function , it's general purpose is to get an array of the depIds under the parent root $depId.

i use recursion method to get the array:

public function getEmpsByDep($depId){
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId;
    $stmt=$this->db->query($query);     
    while(($row=$this->db->fetch_assoc($stmt))==true)
    {   
        if($this->hasChildNode($row['DEPID']))
        {
            $depId = $row['DEPID'];
            self::getEmpsByDep($depId);
        }
        else
        {
            $arr[]=$row['DEPID'];
        }
    }
    return ($arr);
}

here is hasChildNode function to check if specified $depId has child department:

public function hasChildNode($depId)
{
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId;
    $stmt=$this->db->query($query);
    $row=$this->db->fetch_assoc($stmt);
    if($row==false){
        return false;
    }else
        return true;
}

while i think it should return a 1D array of the depid.but when calls:

$this->getEmpsByDep(0);

it return a strange 2D array like this when change "return" to "var_dump" in getEmpsByDep function:

 array(4) {
      [0]=>
      string(2) "11"
      [1]=>
      string(2) "12"
      [2]=>
      string(2) "13"
      [3]=>
      string(2) "14"
    }
    array(3) {
      [0]=>
      string(2) "19"
      [1]=>
      string(2) "20"
      [2]=>
      string(2) "21"
    }
    array(3) {
      [0]=>
      string(2) "15"
      [1]=>
      string(2) "16"
      [2]=>
      string(2) "17"
    }
    array(8) {
      [0]=>
      string(1) "2"
      [1]=>
      string(1) "4"
      [2]=>
      string(1) "5"
      [3]=>
      string(1) "6"
      [4]=>
      string(1) "7"
      [5]=>
      string(1) "8"
      [6]=>
      string(1) "9"
      [7]=>
      string(2) "10"
    }

here is the table structure and data sample:

$query[]="create table ".$sqltblpre."department(
     depId number(10) not null primary key,
     depName varchar2(50) not null,
     id_parent number(10)
)";

//department(部门和岗位)
$index=1;
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'院部',0)";  //1
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'政治部',0)"; //2
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'医务部',0)"; //3
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'护理部',0)"; //4
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'经济部',0)";  //5
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'信息科',0)";  //6
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'医学工程科',0)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'门诊系统',0)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'内科系统',0)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'外科系统',0)";

$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'院长',1)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'政委',1)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'副院长',1)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'秘书',1)";

$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'主任',3)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'副主任',3)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'助理员',3)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'训练队',3)"; //18

$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'队长',18)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'助理员',18)";
$query[] = "INSERT INTO ".$sqltblpre."department values(".$index++.",'队员',18)";

so in a word, how can i get the 1D array thought the right code of this function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪漫之都 2024-09-08 13:35:38

您需要合并数组,此时您再次调用函数但丢弃输出。查看此示例以了解我的意思

<?php
function recursive($x) {
    $arr = array();
    if ($x == 10) {
        return $arr;
    } else {
        $i = $x;
        while ($i != 0) {
            $arr[] = $i;
            $i = $i - 1;
        }

        return array_merge($arr, recursive($x + 1));
    }
}

$arr = recursive(0);

var_dump($arr);

?>

要修复您的代码,请替换

self::getEmpsByDep($depId);

array_merge($arr, self::getEmpsByDep($depId));

You need to merge your arrays, at the moment you're calling your function again but tossing away the output. Look at this example to see what I mean

<?php
function recursive($x) {
    $arr = array();
    if ($x == 10) {
        return $arr;
    } else {
        $i = $x;
        while ($i != 0) {
            $arr[] = $i;
            $i = $i - 1;
        }

        return array_merge($arr, recursive($x + 1));
    }
}

$arr = recursive(0);

var_dump($arr);

?>

To fix your code, replace

self::getEmpsByDep($depId);

With

array_merge($arr, self::getEmpsByDep($depId));
美人迟暮 2024-09-08 13:35:38

在我看来,每次 childnode = true 并且新的迭代开始。将创建一个具有新值的新数组...
但这可能是对我的代码的错误解释:P

It seems to me that everytime the childnode = true and a new itteration starts. There will be created a new array with new values...
But that could be a wrong interpretation of the code of me :P

百思不得你姐 2024-09-08 13:35:38

你可以尝试这个

public function getEmpsByDep($depId)
{ 
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId; 
    $stmt=$this->db->query($query);
    $arr = array();
    while($row=$this->db->fetch_assoc($stmt)) 
    {    
        if($this->hasChildNode($row['DEPID'])) 
        { 
           $emps = $this->getEmpsByDep($depId);  //调用非静态方法一般不用SELF::
           $arr = array_merge($arr, $emps);
        } 
        else 
        { 
           $arr[]=$row['DEPID']; 
         } 
    } 
    return ($arr); 
 } 

You may try this

public function getEmpsByDep($depId)
{ 
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId; 
    $stmt=$this->db->query($query);
    $arr = array();
    while($row=$this->db->fetch_assoc($stmt)) 
    {    
        if($this->hasChildNode($row['DEPID'])) 
        { 
           $emps = $this->getEmpsByDep($depId);  //调用非静态方法一般不用SELF::
           $arr = array_merge($arr, $emps);
        } 
        else 
        { 
           $arr[]=$row['DEPID']; 
         } 
    } 
    return ($arr); 
 } 
说谎友 2024-09-08 13:35:38

对于递归函数,尝试传递 $arr 作为引用

public function getEmpsByDep($depId, &$arr = array()){
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId;
    $stmt=$this->db->query($query);     
    while(($row=$this->db->fetch_assoc($stmt))==true)
    {   
        if($this->hasChildNode($row['DEPID']))
        {
            $depId = $row['DEPID'];
            self::getEmpsByDep($depId);
        }
        else
        {
            $arr[]=$row['DEPID'];
        }
    }
    return ($arr);
}

For recursive functions try passing the $arr as a reference

public function getEmpsByDep($depId, &$arr = array()){
    $query = "select * from ".SQLPREFIX."department where id_parent=".$depId;
    $stmt=$this->db->query($query);     
    while(($row=$this->db->fetch_assoc($stmt))==true)
    {   
        if($this->hasChildNode($row['DEPID']))
        {
            $depId = $row['DEPID'];
            self::getEmpsByDep($depId);
        }
        else
        {
            $arr[]=$row['DEPID'];
        }
    }
    return ($arr);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文