php - How can I echo out this array? -
i don't know how go grabbing data in variable , put foreach loop.
class tree{ private $info = array( array('name'=>'jim','state'=>'nu','company'=>'nu','phone'=>array('cell'=>'5615111111','office'=>'5611111111'),'email'=>array('primary'=>'exs@example.com','ex@there.com')), array('name'=>'joe smith','city'=>'phoenix','phone'=>'4805551111','email'=>'jsmith@some_email.com'), array('name'=>'john doe','city'=>'chandler','company'=>'doe co.','email'=>array('jdoe@gmail.com','personal'=>'email@email.com'),'phone'=>'6025550002') ); }
if you're inside of class, can access private variable using $this->variablename
. example, if use in __construct method, echo names this:
supposing had file called class.tree.php
:
class tree{ private $info = array( array('name'=>'jim','state'=>'nu','company'=>'nu','phone'=>array('cell'=>'5615111111','office'=>'5611111111'),'email'=>array('primary'=>'exs@example.com','ex@there.com')), array('name'=>'joe smith','city'=>'phoenix','phone'=>'4805551111','email'=>'jsmith@some_email.com'), array('name'=>'john doe','city'=>'chandler','company'=>'doe co.','email'=>array('jdoe@gmail.com','personal'=>'email@email.com'),'phone'=>'6025550002') ); public function __construct() { // leaving in references' sake /* foreach ($this->info $elm) { * echo $elm["name"] . "<br/>"; * } **/ } public function getinfo() { return $this->info; } }
now in view (body), use this:
<?php // watch line have file called class.tree.php in same directory! require_once 'class.tree.php'; $tree = new tree(); $info = $tree->getinfo(); ?> <table> <tr> <th>name</th> <th>state</th> <th>city</th> <th>phone</th> </tr> <?php foreach ($info $elm) { ?> <tr> <td><?php echo (isset($elm['name'])) ? $elm['name'] : ""; ?></td> <td><?php echo (isset($elm['state'])) ? $elm['state'] : ""; ?></td> <td><?php echo (isset($elm['city'])) ? $elm['city'] : ""; ?></td> <td> <?php if (isset($elm['phone'])) { if (is_array($elm['phone'])) { foreach ($elm['phone'] $key => $phone) { echo $phone . " ($key)<br/>"; } } else { echo $elm['phone']; } } ?> </td> </tr> <?php } ?> </table>
Comments
Post a Comment