Reputation: 1465
Ok, here we go. I hope I explain this correctly.
I have an object that i'd like to loop through to obtain both the key and value. Here is an example of the object I'm receiving. Thanks in advance for any help or ideas.
Array
(
[0] => stdClass Object
(
[id] => 93
[RecordGUID] =>
[txtEmplid] => 0134754
[txtFname] =>
[txtLname] =>
[txtMname] =>
[txtEmail] =>
[txtSecEmail] =>
[txtPhoneNo] => 4046565454
[drpMajor] =>
[drpStatus] =>
[regmain] =>
[chkDental] => 0
[chkDO] =>
[chkMD] =>
[chkMDPHD] =>
[chkNursin] =>
[chkOPT] =>
[chkPA] =>
[chkPH] =>
[chkPharm] =>
[chkPOD] =>
[chkPostBac] =>
[chkVet] =>
)
)
I basically need to loop through the above info getting both the key and value. For example:
id=93
RecordGUID=
txtEmplid=0134754
and so on.
Again, thanks in advance for any answers.
UPDATE for DBF Here is what I get when I use your code snippt:
int(0)
object(stdClass)#27 (24) {
["id"]=>
string(2) "93"
["RecordGUID"]=>
NULL
["txtEmplid"]=>
string(7) "0134754"
["txtFname"]=>
string(0) ""
["txtLname"]=>
string(0) ""
["txtMname"]=>
string(0) ""
["txtEmail"]=>
string(0) ""
["txtSecEmail"]=>
string(0) ""
["txtPhoneNo"]=>
string(10) "4045506561"
["drpMajor"]=>
NULL
["drpStatus"]=>
NULL
["regmain"]=>
NULL
["chkDental"]=>
string(1) "0"
["chkDO"]=>
NULL
["chkMD"]=>
NULL
["chkMDPHD"]=>
NULL
["chkNursin"]=>
NULL
["chkOPT"]=>
NULL
["chkPA"]=>
NULL
["chkPH"]=>
NULL
["chkPharm"]=>
NULL
["chkPOD"]=>
NULL
["chkPostBac"]=>
NULL
["chkVet"]=>
NULL
}
Upvotes: 10
Views: 38835
Reputation: 3463
use get_object_vars ( object $object )
$vars = get_object_vars ( $object );
foreach($vars as $key=>$value) {
var_dump($key);
var_dump($value);
}
or just iterate the object itself
foreach($object as $key=>$value) {
var_dump($key);
var_dump($value);
}
-- edit 2
Here you'll have the keys and values in one line
$string = "";
foreach($regs as $object) {
foreach($object as $key=>$value) {
$string += "{$key}={$value} ";
}
}
echo $string;
if this is not what you need, I'm clueless ..
Upvotes: 34
Reputation:
You can loop through object properties with foreach
foreach($array as $key => $object)
foreach($object as $property => $value)
echo "{$property} : $value" . PHP_EOL;
Upvotes: 2