Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop OBJECT and obtain both key and value

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
}
like image 392
Richard M Avatar asked Sep 08 '12 23:09

Richard M


People also ask

Can we loop an object which is like key-value pairs?

The Object. entries() method returns an array of a given object's key/value pairs. The for...of loop is used to loop through an array.

Can we loop the object?

In JavaScript, when you hear the term "loop", you probably think of using the various loop methods like for loops, forEach() , map() and others. But in the case of objects, unfortunately, these methods don't work because objects are not iterable.


2 Answers

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 ..

like image 116
dbf Avatar answered Oct 05 '22 22:10

dbf


You can loop through object properties with foreach

foreach($array as $key => $object)
    foreach($object as $property => $value)
        echo "{$property} : $value" . PHP_EOL;
like image 38
P M Avatar answered Oct 06 '22 00:10

P M