简体   繁体   中英

PHP: printing array coming from a class

I'm teaching myself php and I'm stuck with representing a data array coming from a class. Here's Myclass.php:

 class Myclass
    {

        private $iVar1;
        private $strVar2;



        public function __construct() 
        {
            $this->initialize();
        }


        private function initialize()
        {
            $this->iVar1 = null;
            $this->strVar2 = null;
        }

        public function getParam1()
        {
            return $this->iVar1;
        }


        public function setParam1($value)
        {
            $this->iVar1 = $value;
        }


        public function getParam2()
        {
            return $this->strVar2;
        }


        public function setParam2($value)
        {
            $this->strVar2 = $value;
        }


    }

    class MyclassDAO
    {

        static public function load($oBD, $idObjet)
        {

            $aryM = MyclassDAO::getAryM($oBD, $idObjet);

            return $aryM[0];
        }

        static public function getAryM($oBD, $omy)
        {
            $aryCollection = array();

                $sSql = 'EXEC MYDB.dbo.SELECT_MYPROCEDURE ';

                if (!is_null($omy->getParam1()))
                {
                    $sSql .=" @param1 = '".$omy->getParam1()."',";  
                }
                if (!is_null($omy->getParam2()))
                {
                    $sSql .=" @param2 = '".$omy->getParam2()."',";   
                }

                //I delete de last ,
                $sSql =  substr($sSql,0,strlen($sSql)-1); 


                        while ($d = $oBD->fArray()) 
                        { 
                            $omy = new GrupoAlumno();
                            $omy->setParam1($d['param1']);
                            $omy->setParam2($d['param2']);
                            $aryCollection[] = $omy;
                        }

            return $aryCollection;
        }



    }

And here's my php file:

require_once "Myclass.php";
$var1=1;
$var2=’Y’;    
 $om = new Myclass();
            $om->setParam1($var1);
            $om->setParam2($var2);
            $omDAO = new MyclassDAO();
            $ary= $omDAO->load($myObjDB, $om);
// the printing of the info here

The info returned by the class has this structure:

$ary: array=
0:object(Myclass)=
iVar1:long=113
strVar2:string=SOMEINFO
1: object(Myclass)=
iVar1:long=114
strVar2:string=SOMEINFO2

How do I print it in my php file??

Thanks a lot!

// Displays a multi-dimensional array as a HTML unordered lists.
function displayTree($array) {
    $newline = "<br>";
    foreach($array as $key => $value) {    //cycle through each item in the array as key >=> value pairs
      if (is_array($value) || is_object($value)) {        
        //if the VALUE is an array, then
        //call it out as such, surround with brackets, and recursively call displayTree.
        $value = "Array()" . $newline . "(<ul>" . displayTree($value) . "</ul>)" . $newline;
      }
      //if value isn't an array, it must be a string. output its' key and value.
      $output .= "[$key] => " . $value . $newline;
    }
    return $output;
}

This will help you in doing this in a far better and fast way, where system load will be low

只需使用print_r($ ary)或var_dump($ ary)

foreach($ary as $component) {
    echo $component->iVar1;
    echo $component->strVar2
}

This will print the array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM