strange array in php

Posted by tunpishuang on Stack Overflow See other posts from Stack Overflow or by tunpishuang
Published on 2010-05-12T08:28:11Z Indexed on 2010/05/12 8:34 UTC
Read the original article Hit count: 233

Filed under:
|
|

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

while i think it should return a 1D array of the depid.but it return a strange 2D array like this:

 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?

© Stack Overflow or respective owner

Related posts about php

Related posts about recursion