23
Category: PHP
Generous way of returning multidimensional array from php function and getting them individually in program

Working with multidimensional array in php when they are returning from a function is a frequent need in php program. But declaring the return as array with the desired variables to return make the job easy with list() function. This is a regular practice of PHP programmer but for the beginners the piece of following code may become usable.

Here the code is given from a program used in an employee managing software where the database is introduced as ODBC connection. But the way is applicable in any PHP program as required. 

Storing more than one variable as array by fetching the database in a function.

//// put this in function.php  ///////////

function listofemplinarray()

{

include("connectdb.php");

//$conn_univ=odbc_connect("roster_univ","","");

$str_a="SELECT * FROM DUMMYMASTER ORDER BY EMPCODE";

$rs_a=odbc_exec($conn_univ,$str_a);

 

                while(odbc_fetch_row($rs_a))

                {

                                                $pf[]=odbc_result($rs_a,"EMPCODE");

                                                $name[]=odbc_result($rs_a,"NAME");

                }

                return array($pf,$name);

 //return $pf;

}

 

Get the array variables returned by function

//// put this in Employee.php  ///////////

list($x,$y)=listofemplinarray();

foreach ( $x as $key=>$value)

                {

                echo $value."-".$y[$key]."<br>";

                }