Those are all basically member variables of a class, so you can access it via standard [man]oop[/man] methods; $from->from[0]->personal should do the trick.
[RESOLVED] Array echo
Cheers, what about a loop for each item in a array
[0] => stdClass Object
(
[personal] => 2GB SPORT
[mailbox] => sport
[host] => 2gb.com
)
[1] => stdClass Object
(
[personal] => AAP
[mailbox] => jancetics
[host] => aap.com.au
)
[2] => stdClass Object
(
[personal] => Adam Hamilton
[mailbox] => hamiltona
[host] => heraldsun.com.au
)
How do i do this?
Looks like just a standard array, so just use [man]foreach/man on the array variable.
echo "<To><![CDATA[";
foreach ($to as $i => $value) {
echo " " $to[0]->mailbox . "@" . $to[0]->host . ";";
}
echo "]]></To>";
This does not work though?
Well... that's because you didn't exactly use [man]foreach/man correctly. First, are you sure $to is the variable that contains the array you showed above? If so, then you'd do something like:
foreach($to as $data) {
// $data is now a stdClass Object
}
echo "<To><![CDATA[";
foreach ($to as $data) {
echo " " $data[0]->mailbox . "@" . $data[0]->host . ";";
}
echo "]]></To>";
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/xuser/public_html/mypage.php on line 61
I know what this error is saying but can not see the issue?
Missing a period between the first string piece and the first variable.
Also, why did you use $data[0]? If $to is the array you showed above, then $data won't be an array - it's just an object.
Sorry I worked it out
echo "<To><![CDATA[";
foreach ($to as $data) {
echo " " . $to[0]->mailbox . "@" . $to[0]->host . ";";
}
echo "]]></To>";
Problem is if there is more than one it does not work, if there is just 1 (0) then it works...
Do a [man]var_dump/man on the $to variable and show us its structure.
It goes on longer than this but this is the dump
array(173) {
[0]=>
object(stdClass)#363 (3) {
["personal"]=>
string(9) "2GB SPORT"
["mailbox"]=>
string(5) "sport"
["host"]=>
string(7) "2gb.com"
}
[1]=>
object(stdClass)#259 (3) {
["personal"]=>
string(3) "AAP"
["mailbox"]=>
string(9) "jancetics"
["host"]=>
string(10) "aap.com.au"
}
[2]=>
object(stdClass)#272 (3) {
["personal"]=>
string(13) "Adam Hamilton"
["mailbox"]=>
string(9) "hamiltona"
["host"]=>
string(16) "heraldsun.com.au"
}
[3]=>
object(stdClass)#281 (3) {
["personal"]=>
string(10) "Adam Hawse"
["mailbox"]=>
string(6) "ahawse"
["host"]=>
string(17) "networkten.com.au"
}
Right, so go back to my example; the point of using [man]foreach[/man]($to as $data) is that the foreach() construct will place each array entry into $data after each iteration. It's basically a while() loop that loops through all array indexes one-by-one.
Since you still used $to inside the foreach() loop, though, you basically ignored the whole point of the foreach() statement and accessed the array directly. Instead, you should be using $data (without any array index, since $data will reference the contents of the array one-by-one, and the contents are objects).
So ...
echo "<To><![CDATA[";
foreach ($to as $data) {
//foreach ($to as $i => $value) {
echo " " . $data->mailbox . "@" . $data->host . ";";
}
echo "]]></To>";
Based on the var_dump() above, yes, that seems right. Did it work?
Yes i did, thanks so much for your help.