Reputation: 136
I don't know how I would go about grabbing the data in the variable and put it into a foreach loop.
class Tree{
private $info = array(
array('name'=>'Jim','state'=>'NU','company'=>'NU','phone'=>array('cell'=>'5615111111','office'=>'5611111111'),'email'=>array('primary'=>'[email protected]','[email protected]')),
array('name'=>'Joe Smith','city'=>'Phoenix','phone'=>'4805551111','email'=>'jsmith@some_email.com'),
array('name'=>'John Doe','city'=>'Chandler','company'=>'Doe Co.','email'=>array('[email protected]','personal'=>'[email protected]'),'phone'=>'6025550002')
);
}
Upvotes: 0
Views: 74
Reputation: 6537
If you're inside of the class, you can access your private variable using $this->variableName
. For example, if you use it in the __construct method, you could echo all the names like this:
Supposing you had this 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'=>'[email protected]','[email protected]')),
array('name'=>'Joe Smith','city'=>'Phoenix','phone'=>'4805551111','email'=>'jsmith@some_email.com'),
array('name'=>'John Doe','city'=>'Chandler','company'=>'Doe Co.','email'=>array('[email protected]','personal'=>'[email protected]'),'phone'=>'6025550002')
);
public function __construct() {
// Leaving this in for references' sake
/* foreach ($this->info as $elm) {
* echo $elm["name"] . "<br/>";
* }
**/
}
public function getInfo() {
return $this->info;
}
}
Now in your view (body), you could use something like this:
<?php
// Watch this line that you really have a file called Class.Tree.php in the 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 as $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'] as $key => $phone) {
echo $phone . " ($key)<br/>";
}
} else {
echo $elm['phone'];
}
} ?>
</td>
</tr>
<?php } ?>
</table>
Upvotes: 1
Reputation: 2871
public function show() {
foreach ($this->info as $node) {
foreach ($node as $key => $value) {
echo "key = " . $key . ", value = " . $value . PHP_EOL; // $value may be Array
}
}
}
Upvotes: 0
Reputation: 2827
Assuming that you are using the variable in the same class you can do this.
$arrVal = array();
$arrVal = $info;
foreach ($arrVal as $val)
{
foreach($val as $sing)
{
//access the value of each array with index. Eg: $sing['name']
}
}
I hope this helps you
Upvotes: 1